简体   繁体   中英

Python Tkinter OOP Layout Configuration

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.

The layout is as shown in the pic below. (1280x720px) 在此处输入图像描述


I have the following:

  • banner on top with username/welcome message, and logo rh corner, columnspan=8
  • menu bar with buttons on the left, split into 2 rows (row1: rowspan 6, row2: rowspan=4)
  • working area (white block) that has a Frame, which I'll add a notebook to, each menu button opening a different notebook page.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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