Python Tkinter OOP 布局配置

2024-05-07

我正在尝试使用 tkinter 构建一个应用程序。 该布局在没有 OO 原则的情况下工作,但我很难理解应该如何将其转移到 OO。

The layout is as shown in the pic below. (1280x720px) enter image description here


我有以下内容:

  • 顶部横幅,带有用户名/欢迎消息和徽标右角,columnspan = 8
  • 左侧带有按钮的菜单栏,分为 2 行(row1:rowspan 6,row2:rowspan=4)
  • 工作区域(白色块)有一个框架,我将向其中添加一个笔记本,每个菜单按钮打开一个不同的笔记本页面。

实现这个 OO 的最佳方式是什么? (我还在学习中,所以对 OO 很陌生)


不可能直接翻译,因为一切都取决于您的需求。 如果您创建一个简单的程序,您只需创建类并在构造函数中创建每个按钮、标签、框架...。创建时,您必须选择布局管理器网格、包或放置之一。之后你创建你的函数就完成了。如果您处理更大的项目并且有大量标签、按钮等,您可能想为每个项目创建容器。 在您的情况下,您不需要很多功能和按钮,因此您应该采用基本方法:

from tkinter import *

class name_gui:
    def __init__(self, top):

#specify main window

        self.top = top
        self.title = top.title("name_gui")
        self.minsize = top.geometry("1280x720")
        self.resizable = top.resizable(height=False,width=False)

#create buttons,Labels,Frames..

        self.Button1 = Button(top,text="Button1",command=self.exa_fun)
        self.Button2 = Button(top,text="Button2",command=self.exa_fun2)

#place them, choose grid/place/pack

        self.Button1.place(relx=0.5,rely=0.5)
        self.Button2.place(relx=0.5,rely=0.2)


#create your functions
    
    def exa_fun(self):
        pass

    def exa_fun2(self):
        pass


top = Tk()
exa_gui = name_gui(top)
top.mainloop()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python Tkinter OOP 布局配置 的相关文章

随机推荐