I Have used Python Tkinter Is There a Way to use multiple windows in tkinter Can Any one tell pe Please
3 Answers
You can use tk.Toplevel() to create new window in tkinter.
More information is available here
Example
new_win=Toplevel()
Note: if you destroy the main Tk() all of the Toplevel() attached to that main window will also be destroyed.
1 Comment
TheLizzard
It might be worth adding a comment that says something like: if you destroy the main
Tk() all of the Toplevel()s attacked to that main window will also be destroyed.I believe an example is in order.
import tkinter as tk
# primary tkinter window
parent = tk.Tk()
parent.title( "Parent" )
# A child window can be created like so
child = tk.Toplevel( parent )
child.transient( parent )
child.title( "Child" )
# Note the reference to parent when child is created
# The transient method will guarantee the child will ALWAYS be in front of the parent
# Any number of children can be created for every parent
# Complex widgets that have many components require time to instantiate correctly
parent.update_idletasks()
# When a parent is destroyed so will the children
def closer( event ):
parent.destroy()
# Widgets can be bound to keyboard or mouse inputs
parent.bind( "<Escape>", closer )
parent.mainloop()
7 Comments
TheLizzard
The
parent.update_idletasks() is useless. All of the things it will do will be done when the code execution reaches parent.mainloop().Derek
@TheLizzard the rem statement mentions complex widgets.
TheLizzard
What widget would need that? All widgets that are part of
tkinter and tkinter.ttk don't need .update_idletasks() to be created correctly. Also can you give an example of a complex widget?Derek
Something with lots of images, scrollbars, children, buttons etc...
TheLizzard
Still doesn't need
.update_idletasks(). If you have spare time can you please give me a basic minimal example (on something like pastebin) where removing the .update_idletasks() will be a problem? Also the documentation never mentions that it initialises the widget/children of that widget. |
You can have multiple instance of Tk() -
root = Tk()
win = Tk()
But multiple instances of Tk() are discouraged, see why
The best solution is Top Levels This is how you make a toplevel widget -
toplevel = Toplevel(root, bg, fg, bd, height, width, font, ..)
2 Comments
PCM
Windows and tabs have different meanings right? So I thought OP wanted multiple windows and not tabs