[英]Python Tkinter; .place() not working but .grid() and .pack() do
I am trying to insert a Label with text on a frame.我正在尝试在框架上插入带有文本的标签。 However, it does not appear with .place, even though it works fine with .grid and .pack.但是,它不会与 .place 一起出现,即使它与 .grid 和 .pack 一起工作正常。
label = tk.Label(master, text="test")
label.pack() # works
label = tk.Label(master, text="test")
label.grid() # works
label = tk.Label(master, text="test")
label.place(relx=.5, rely=.5) # doesn't work
label = tk.Label(master, text="test")
label.place(relx=.5, rely=.5, width=500, height=500) # doesn't work
To be clear, I am not mixing the 3 methods.需要明确的是,我没有混合这 3 种方法。 I'm running each attempt by separately.我正在分别进行每次尝试。
My code structure is as follows:我的代码结构如下:
class Window(tk.Tk)
def __init__(self):
tk.Tk.__init__(self)
frame_init = Frame_0(self, 0)
frame_init.pack()
class Frame(tk.Frame)
def __init__(self, Window)
tk.Frame.__init__(self, Window)
class Frame_0(Frame):
def __init__(self, Window)
super().__init__(Window)
label = tk.Label(self, text="test")
label.METHOD() # METHOD is pack, grid, or place ()
... somewhere else
Window = Window()
Window.mainloop()
When you use place
, the size of the child won't affect the size of the master.使用place
,child 的大小不会影响 master 的大小。 Since you didn't specify a size for your frame its size will be 1x1.由于您没有为框架指定尺寸,因此其尺寸将为 1x1。 Your label is in the frame, but the frame is virtually invisible.您的标签是在框架,但框架是几乎看不见。
If you're going to use place
, it's up to you to make sure that you are placing the widget inside a visible widget.如果您要使用place
,则由您来确保将小部件放置在可见的小部件中。
Try anchoring method, it may be hiding inside试试锚定法,可能藏在里面
label.place(anchor = CENTER)
Maximize you window and check whether it's hiding somewhere too最大化您的窗口并检查它是否也隐藏在某处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.