tkinter的.pack_propagate()方法 [英] tkinter's .pack_propagate() method

查看:192
本文介绍了tkinter的.pack_propagate()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Tkinter,因为我试图弄清是否可以在不使用画布的情况下设置tkinter的窗口大小.我想到了如何在SO的问题与解决方案"上设置框架尺寸问题回答.因此,我通过编写一个很小的程序来显示文本标签来对其进行测试.但是我发现它是丢失"的,或者当我使用 frame.pack_propagate(0)

I am experimenting with Tkinter, as I was trying to figure out is there a way to set the tkinter's window size without using canvas. I came upon this how to set frame size question on SO's Question & Answer. So I went ahead and test it by writing a very small program to display a text label. But I found out it is "missing", or disappear when I use 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()

所以我的问题是,您能解释为什么当我使用 frame.pack_propagate(0)而不是 frame.pack()时为什么我的textBox(Label)没有出现>方法?其次,有没有一种方法可以在不使用画布的情况下设置窗口大小?我想知道,因为在向我介绍朋友之前,我正在编写一系列小程序来教我的朋友关于tkinter的知识.如果在我的tkinter样本中窗口大小全部相同,那就太好了.我也想知道(好奇).非常感谢.

So my question is, can you explain why my textBox (Label) is not appearing when I use the frame.pack_propagate(0) instead of frame.pack() method? And secondly, is there a way to set the window size without using a canvas? I want to know because I am writing a series of small programs to teach my friend about tkinter, before introducing canvas to him. It would be nice if the window size are all the same across my tkinter samples. And I am just wondering as well (curious). Thank you very much.

我正在MAC OS 10.5.8上使用python 3.2.2.

I am using python 3.2.2 on MAC OS 10.5.8.

推荐答案

pack_propagate 仅设置一个标志,不会导致将框架放置在小部件中.它不能替代调用 pack .

pack_propagate only sets a flag, it doesn't cause the frame to be placed in the widget. It is not a substitute for calling pack.

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

In other words you must do this:

# 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()

这篇关于tkinter的.pack_propagate()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆