简体   繁体   中英

tk: do I need to mention the widget master?

I want to rearrange frames in PanedWindows at runtime based upon user request. Previously I specified the master for each frame (here pw1). Ex:

import Tkinter as tk

root = tk.Tk()

pw1 = tk.PanedWindow(orient=tk.HORIZONTAL, sashpad=2,
    sashwidth=4, sashrelief=tk.RAISED)
pw1.pack(fill=tk.BOTH, expand=1)

frame1 = tk.Frame(pw1, bg='red',width=100, height=100) 
frame2 = tk.Frame(pw1, bg ='yellow',width=100,height=100)

# This seems to do the same thing:
# frame1 = tk.Frame(bg='red',width=100, height=100) # vs. tk.Frame()
# frame2 = tk.Frame(bg ='yellow',width=100,height=100) 

pw1.add(frame1, sticky=tk.NSEW)
pw1.add(frame2, sticky=tk.NSEW)

root.mainloop()

What is the advantage of specifying the master?

The master paremeter can be interpreted as parent . Passing this will add a new key in the parent.children attribute.

If you print pw1.children when you do not pass master AND do not execute pw1.add() you will see that it returns an empty dictionary.

print pw1.children
#{}

The pw1.add() method also includes the added widget in the pw1.children dictionary. I still did not understand why when you pass master the widgets are not automatically shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM