Python Tkinter OOP 布局配置 [英] Python Tkinter OOP Layout Configuration

查看:37
本文介绍了Python Tkinter OOP 布局配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tkinter 构建应用程序.布局在没有面向对象原则的情况下工作,但我正在努力理解我应该如何将其移动到面向对象.

I am trying to build an application with tkinter. The layout works without OO principles, but I am struggling to understand how I should move it to OO.

布局如下图所示.(1280x720 像素)

The layout is as shown in the pic below. (1280x720px)

我有以下几点:

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

制作这个 OO 的最佳方法是什么?(我还在学习,所以对 OO 很陌生)

What is the best way to make this OO? (I am still learning, so very new to OO)

推荐答案

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

There is no straight translation possible, since everything depends on your needs. If you create a simple programm you can just create the class and create every Button,Label,Frame... in the constructor. When created you have to choose one of the layout managers grid,pack or place. After that you create your functions and you are done. If you deal with bigger projects and have a big amount of Labels, Buttons etc.. you maybe want to create containers for each. In your case you wont need a lot of functions and buttons, so you should maybe go with the basic approach:

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()

这篇关于Python Tkinter OOP 布局配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆