1

I am trying to improve my structure of tkinter apps.

Therefore, I tried the OOP approach mentioned in this thread.

I tried to play around with it, but couldn't get much further than the example code. I just need a status bar and a main body, but I am unable to display anything (e.g. a Label, Frame, Canvas, Button, ...) in the statusbar/main class.

This is what I currently have:

import tkinter as tk

class Statusbar(tk.Frame):
    #create Labels, Buttons,...

class Main(tk.Frame):
    #create Labels, Buttons,...


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.statusbar = Statusbar(self)
        self.main = Main(self)
        self.statusbar.grid()
        self.main.grid()


if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).grid()
    root.mainloop()

Maybe you can point me in the right direction or show me a simple example. Thank you.

EDIT: Trying to be more specific:

I have worked with tkinter/python a bit. I just don't know how to proceed with the proposed OOP structure from the above link. I can't get anything inside the Statusbar and Main class to work (like: creating a label, a button, and so on). So, if you could show me a simple example to create anything in those classes, I'd be good to go.

martineau
  • 119,623
  • 25
  • 170
  • 301
X_841
  • 191
  • 1
  • 14
  • @X_841 ***"can't get anything inside the `Statusbar`"***: As it stands, `class Statusbar` is a invalid class defenition. Take the tour [Python - Object Oriented](https://www.tutorialspoint.com/python/python_classes_objects.htm) and extend to a valid class defenition. – stovfl Jan 12 '20 at 21:00

1 Answers1

2

Here's something that should give you the general idea:

import tkinter as tk


class Statusbar(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        lbl1 = tk.Label(self, text='Status1', fg='yellow', bg='blue')
        lbl1.pack(side='left')
        lbl2 = tk.Label(self, text='Status2', fg='white', bg='green')
        lbl2.pack(side='left')


class Main(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        txt = tk.Text(self, width=15, height=5)
        txt.insert(tk.END, 'Hello world')
        txt.pack()


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.statusbar = Statusbar(self)
        self.main = Main(self)

        self.statusbar.grid()
        self.main.grid()


if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).grid()
    root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301