简体   繁体   English

Python Tkinter使用grid()设置小部件框架

[英]Python Tkinter set widgets frame with grid()

My problem is this. 我的问题是这个。 I create a tkinter widget and later down the road I create a new frame that I want to add this widget to. 我创建了一个tkinter小部件,随后创建了一个新框架,我想将该小部件添加到其中。 When I call .grid() on the widget the widget is placed on the first frame, not the newer one that I want it to be on. 当我在窗口小部件上调用.grid()时,窗口小部件将放置在第一帧上,而不是我希望放置在其上的新版本上。

By default a widget is managed by its parent. 默认情况下,小部件由其父级管理。 If you don't want that, use the parameter in_ when calling pack , place or grid . 如果不希望这样, in_在调用packplacegrid时使用in_参数。

For example: 例如:

self.f1 = tk.Frame(...)
self.label = tk.Label(self.f1, ...)
self.label.pack(...)
...
self.f2 = tk.Frame(...)
self.label.pack(in_=self.f2, ...)

However, if you find yourself doing this a lot, you're probably doing something wrong. 但是,如果您发现自己做了很多事情,则可能是您做错了什么。 This is almost never necessary in most tkinter applications. 在大多数tkinter应用程序中,这几乎是没有必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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