Toplevel Child Window example (Tkinter/Python)

vegaseat 0 Tallied Votes 6K Views Share

A closer look at the Tkinter GUI toolkit Toplevel Window and how to lift and lower it respective to other windows.

''' tk_Toplevel2.py
a toplevel child window has options like color, height, and width
size and position it with geometry()
lift and lower it with respect to other windows
you can exit a toplevel window alone
or it will exit as the main/root window exits
tested with Python27 and Python33  by vegaseat  10dec2012
'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def lift_win1():
    win1.lift(aboveThis=root)

def lower_win1():
    win1.lower(belowThis=root)

root = tk.Tk()

root.title('root win')
root.geometry("200x100+30+30")
root.configure(bg='yellow')

# create a child/top window
win1 = tk.Toplevel(bg='red')
win1.title('top/child window win1')
win1.geometry("300x150+120+120")

btn_lift = tk.Button(win1, text="Lift win1", command=lift_win1)
btn_lift.pack(padx=30, pady=5)
btn_lower = tk.Button(win1, text="Lower win1", command=lower_win1)
btn_lower.pack(pady=5)

win1.mainloop()