在Python中使用儿童课时,TKINTER网格放置如何工作?

发布于 2025-02-10 08:48:07 字数 1727 浏览 1 评论 0原文

为了序言,我有这个工作,我只是希望有人可以解释为什么代码的行为方式。我显然不太了解课堂内部的网格系统。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2025-02-17 08:48:08

考虑此代码:

class HeaderFrame(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

super().__ INT __()实际上创建了小部件。您不会将parent传递给super().__ INT __()),因此将创建小部件作为root窗口的孩子。您需要通过以下示例中传递parent

super().__init__(parent)

您需要为所有课程做出类似的更改。

Consider this code:

class HeaderFrame(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

super().__init__() is what actually creates the widget. You aren't passing parent to super().__init__()) so the widget will be created as a child of the root window. You need to pass parent like in the following example.

super().__init__(parent)

You need to make a similar change for all of your classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文