B
B
boldensiy2017-03-22 20:51:07
Python
boldensiy, 2017-03-22 20:51:07

OOP in Python. How to organize a class with a parent window and a class with a child window?

Actually there is such a code thrown by me. Initially, I had a task: Create an application using tkinter and an OOP approach to building a GUI, so that there is a main window and a child that is called by clicking on the button. I'm a beginner, but after googling and reading the literature, my solution resulted in such a code. There is a main class Main which is inherited from Frame and is a parent window, there is also a class Child which is inherited from Toplevel which in turn is a child window.
The question is how correctly I implemented this task from a professional point of view. I want the opinions of experienced Pythonists, this task pops up for many in one implementation or another. There is a desire to create a basic template in order to further continue the implementation of your own application.

import tkinter as tk
 
 
class Main(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.init_main()
 
    def init_main(self):
        toolbar = tk.Frame(bg='#d7d8e0', bd=2)
        toolbar.pack(side=tk.TOP, fill=tk.X)
 
        btnOpenDialog = tk.Button(toolbar, text='Добавить позицию', command=self.open_dialog, bg='#d7d8e0', bd=0, compound=tk.TOP)
        btnOpenDialog.pack(side=tk.LEFT)
 
    def open_dialog(self):
       Child()
 
 
class Child(tk.Toplevel):
    def __init__(self):
        super().__init__(root)
        self.title('Добавить доходы/расходы')
        self.geometry('400x220+400+300')
        self.resizable(False, False)
 
if __name__ == "__main__":
    root = tk.Tk()
    app = Main(root)
    app.pack()
    root.title("Домашние финансы")
    root.geometry("650x450+300+200")
    root.resizable(False, False)
    root.mainloop()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question