tkinter的.pack_propagate()方法

Geo*_*rge 3 python label tkinter

我正在尝试使用Tkinter,因为我试图弄清楚有没有办法在不使用画布的情况下设置tkinter的窗口大小.我想到了如何在SO的问答中设置帧大小问题.所以我继续通过编写一个非常小的程序来测试它,以显示文本标签.但我发现它"缺失",或者在我使用时消失frame.pack_propagate(0)

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root, width=400, height=400)
# Does not work at the moment, textBox is missing
# frame.pack_propagate(0) 
frame.pack()

textBox = tk.Label(frame, text="(x,y): ")
textBox.pack()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,你能解释为什么我使用frame.pack_propagate(0)替代frame.pack()方法时我的textBox(Label)没有出现吗?其次,有没有办法在不使用画布的情况下设置窗口大小?我想知道,因为我正在写一系列小程序来教我的朋友关于tkinter,然后向他介绍画布.如果我的tkinter样本的窗口大小都相同,那将是很好的.我也想知道(好奇).非常感谢你.

我在MAC OS 10.5.8上使用python 3.2.2.

Bry*_*ley 12

pack_propagate只设置一个标志,它不会导致框架放在小部件中.它不能代替召唤pack.

换句话说,你必须这样做:

# put the frame in its parent
frame.pack()

# tell frame not to let its children control its size
frame.pack_propagate(0)

# put the textbox in the frame
textBox.pack()
Run Code Online (Sandbox Code Playgroud)