Skip to content Skip to sidebar Skip to footer

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 th

Solution 1:

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

Post a Comment for "Python Tkinter Oop Layout Configuration"