在Python中使用儿童课时,TKINTER网格放置如何工作?
为了序言,我有这个工作,我只是希望有人可以解释为什么代码的行为方式。我显然不太了解课堂内部的网格系统。
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('400x400')
self.rowconfigure(0, uniform=True, weight=1)
self.rowconfigure(1, uniform=True, weight=1)
self.rowconfigure(2, uniform=True, weight=1)
self.header_frame = HeaderFrame(self)
self.header_frame.grid(column=0,row=0)
self.login_frame = LoginFrame(self)
self.login_frame.grid(column=0,row=1)
self.button_frame = ButtonFrame(self)
self.button_frame.grid(column=0,row=2)
class HeaderFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.columnconfigure(0, uniform=True, weight=1)
self.canvas = tk.Canvas(self, bg='black')
self.canvas.grid(column=0,row=0)
class loginFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent=parent
self.columnconfigure(0, uniform=True, weight=1)
self.entryBox = ttk.Entry(self, width=33)
self.entryBox.grid(column0,row1)
class ButtonFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent=parent
self.columnconfigure(0, uniform=True, weight=1)
self.btn = ttk.Button(self, text='test')
self.btn.grid(column=0,row=2)
if __name__ == '__main__':
App().mainloop()
现在,我的问题归结为网格放置。当我打电话给每个班级以放置项目时,我指定了不同的网格位置。 header_frame.grid(列= 0,行= 0),login_frame(列= 0,行= 1)等。但是,具有网格放置的初始应用程序类不会影响GUI的布局。我可以将它们全部放在第0行上,它们都会仍然显示在不同的行上,直到我更改单个类内部的网格放置。我以为我的班级内部的网格被放置在应用程序类的单元格内,我在这里缺少什么?
事先感谢您的帮助。
To preface this, I have this working, I was just hoping somebody could explain why the code is behaving the way it is. I'm not understanding the grid system inside of classes very well apparently.
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('400x400')
self.rowconfigure(0, uniform=True, weight=1)
self.rowconfigure(1, uniform=True, weight=1)
self.rowconfigure(2, uniform=True, weight=1)
self.header_frame = HeaderFrame(self)
self.header_frame.grid(column=0,row=0)
self.login_frame = LoginFrame(self)
self.login_frame.grid(column=0,row=1)
self.button_frame = ButtonFrame(self)
self.button_frame.grid(column=0,row=2)
class HeaderFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.columnconfigure(0, uniform=True, weight=1)
self.canvas = tk.Canvas(self, bg='black')
self.canvas.grid(column=0,row=0)
class loginFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent=parent
self.columnconfigure(0, uniform=True, weight=1)
self.entryBox = ttk.Entry(self, width=33)
self.entryBox.grid(column0,row1)
class ButtonFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent=parent
self.columnconfigure(0, uniform=True, weight=1)
self.btn = ttk.Button(self, text='test')
self.btn.grid(column=0,row=2)
if __name__ == '__main__':
App().mainloop()
Now my question comes down to the grid placement. When I call each class to place the items, I specify different grid locations. header_frame.grid(column=0, row=0), login_frame(column=0,row=1), etc. However, the initial App class having a grid placement does not affect the layout of the gui whatsoever. I can place them all on row 0 and they will all still show up on separate rows until I change the grid placement inside of the individual class. I thought my grid inside of the class was being placed inside of cells of the App class, what am I missing here?
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑此代码:
super().__ INT __()
实际上创建了小部件。您不会将parent
传递给super().__ INT __())
,因此将创建小部件作为root窗口的孩子。您需要通过以下示例中传递parent
。您需要为所有课程做出类似的更改。
Consider this code:
super().__init__()
is what actually creates the widget. You aren't passingparent
tosuper().__init__())
so the widget will be created as a child of the root window. You need to passparent
like in the following example.You need to make a similar change for all of your classes.