Search code examples
pythonooptkinter

Tkinter OOP for one window with multiple subframes


I wrote a GUI in Tkinter with a lot of widgets. The GUI works but it's quite confusing based on this many variables. So I thought and researched if it's maybe easier to put the subframes in subclasses.

In principle my Gui is structured like this: Image

A similar question is asked here (Link) but the responses go more into the direction of structuring the GUI more into the widgets instead of the sections. That's why I'm not so sure if I'm on the right path. I tested it with the following code but have already has mysterious problems:

import tkinter as tk
from tkinter import ttk

class main_window(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # Adding a title to the window
        #self.wm_title("LiL-SimReg")
        self.geometry("800x1000")
        self.title(" xxx ")
        
        INPUTframe(self)
        
class INPUTframe(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        tk.Frame(self, width=800, height=150).place(x=0,y=0)

        
        label10 = tk.Label(self, text="xxx", font=('Arial',12)).place(x=10,y=0)
        label1 = tk.Label(self, text="xxx", font=('Arial',10)).place(x=30, y=30)
        button1 = tk.Button(self, text='xxx').place(x=100,y=30)#,command = lambda: path(1))
        textentry1=tk.StringVar()
        entry1 = tk.Entry (self, textvariable=textentry1).place(x=160,y=30,width="350")
        label11 = tk.Label(self, text="xxx", font=('Arial',10)).place(x=500,y=30)


        label2 = tk.Label(self, text="xxx", font=('Arial',10)).place(x=30,y=60)
        button2 = tk.Button(self, text='xxx').place(x=100,y=60)#,command = lambda: path(2))
        textentry2=tk.StringVar()
        entry2 = tk.Entry (self, textvariable=textentry2).place(x=160,y=63,width="350")
        label21 = tk.Label(self, text="xxx", font=('Arial',10)).place(x=500,y=60)
        textentry21=tk.DoubleVar(value='x')
        entry21 = tk.Entry (self, textvariable=textentry21).place(x=580,y=63,width="30")

        label3 = tk.Label(self, text="xxx", font=('Arial',10)).place(x=30,y=90)
        button3 = tk.Button(self, text='xxx').place(x=100,y=90)#,command = lambda: path(3))
        textentry3=tk.StringVar()
        entry3 = tk.Entry (self, textvariable=textentry3).place(x=160,y=93,width="350")
        label31 = tk.Label(self, text="xxx", font=('Arial',10)).place(x=500,y=90)
        textentry31=tk.DoubleVar(value='x')
        entry31 = tk.Entry (self, textvariable=textentry31).place(x=580,y=93,width="30")
        

if __name__ == "__main__":
    testObj = main_window()
    testObj.mainloop()

If I use .place for the widgets it works sometimes. Better it works with .pack but also not always so it seems my approach is wrong. There is no error it just doesn't or only randomly depicts the widgets.

Could you give me input if the approach makes sense and what is the mistake in my code?


Solution

  • To position the INPUTframe, you need to update its __init__.

    class INPUTframe(tk.Frame):
        def __init__(self, parent, width=800, height=150):
            tk.Frame.__init__(self, parent, width=width, height=height)
            self.place(x=0, y=0)
    
            label10 = tk.Label(self, text="xxx", font=('Arial',12)).place(x=10,y=0)
    ...