简体   繁体   English

Tkinter网格布局小部件不在父级中

[英]Tkinter Grid Layout Widget not in parent

I am building a GUI in Python with Tkinter however I am having issues with the Grid layout. 我正在使用Tkinter在Python中构建GUI,但是网格布局存在问题。 Am I right in assuming that if you specify a parent, that child then sits at the grid position you specify in that parent? 我是否可以假设,如果您指定一个父母,那么那个孩子坐在您在那个父母中指定的网格位置? Here is my example: 这是我的示例:

layout_frame = tk.Frame(master, bg="red").grid(row=1, column=2, rowspan=9, sticky=tk.N+tk.S+tk.W+tk.E)
inner_frame = tk.Frame(master=layout_frame, bg="red").grid(row=0, column=0, rowspan=9, sticky=tk.N+tk.S+tk.W+tk.E)

My problem is that inner_frame is sitting at grid position (0,0) in master, and not in layout_frame . 我的问题是inner_frame在master中位于网格位置(0,0),而不是在layout_frame Why is it not sitting at position (0,0) in it's specified parent? 为什么它不位于指定父级的位置(0,0)?

Thanks 谢谢

layout_frame is the result of the call to grid (which is None and not the frame you have just created). layout_frame是调用grid的结果(它是None而不是刚创建的框架)。 When the parent of a widget is None, Tkinter sets the parent as the root element, so you have to create the frame first and call grid in a separate statement. 当小部件的父级为None时,Tkinter会将父级设置为根元素,因此您必须首先创建框架并在单独的语句中调用grid

layout_frame = tk.Frame(master, bg="red")
layout_frame.grid(row=1, column=2, rowspan=9, sticky=tk.N+tk.S+tk.W+tk.E)
inner_frame = tk.Frame(master=layout_frame, bg="red")
inner_frame.grid(row=0, column=0, rowspan=9, sticky=tk.N+tk.S+tk.W+tk.E)

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

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