1

I Have used Python Tkinter Is There a Way to use multiple windows in tkinter Can Any one tell pe Please

3 Answers3

3

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.

  • 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. – TheLizzard Jul 17 '21 at 09:10
1

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, ..)
PCM
  • 2,881
  • 2
  • 8
  • 30
0

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()
Derek
  • 1,916
  • 2
  • 5
  • 15
  • The `parent.update_idletasks()` is useless. All of the things it will do will be done when the code execution reaches `parent.mainloop()`. – TheLizzard Jul 17 '21 at 09:09
  • @TheLizzard the rem statement mentions complex widgets. – Derek Jul 17 '21 at 09:27
  • 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*? – TheLizzard Jul 17 '21 at 09:32
  • Something with lots of images, scrollbars, children, buttons etc... – Derek Jul 17 '21 at 09:33
  • Still doesn't need `.update_idletasks()`. If you have spare time can you please give me a basic minimal example (on something like [pastebin](https://pastebin.com/)) where removing the `.update_idletasks()` will be a problem? Also the documentation never mentions that it initialises the widget/children of that widget. – TheLizzard Jul 17 '21 at 09:36
  • You are entitled to your opinion – Derek Jul 17 '21 at 09:37
  • Please look at [this](https://stackoverflow.com/a/29159152/11106801). `.update_idletasks` is very similar to `.update` as in, it handles things like `.after` scripts. `.update()` does the same thing but it also handles other events as well. `.mainloop()` is very similar to a loop that just calls `.update()`. Therefore your `.update_idletasks` is unneeded and the comment is above that wrong. – TheLizzard Jul 17 '21 at 09:43