Object oriented Tkinter

Viewed 76

I am trying to write a gui which as a class which is the main application. A single instance of that class is created in the main root. At the same time I want a submit button to be clicked where some values are verified before a further submission to write the data. I am trying to do this by creating a new class for the Toplevel pop up window. But I am not sure how best to structure this. Ideally an instance of the pop up window class would be created each time the button is selected. It seems like with the way I have structured it another instance of the main application class has been created. I am a little confused how to correctly do this using OOP.

Below is some sample code to illustrate the problem.

import tkinter as tk
from tkinter import ttk

class Window(tk.Frame):

  def __init__(self, master=None):
    
      tk.Frame.__init__(self, master)
    
      self.title = "TITLE"
    
      self.master = master
    
      self.submit = ttk.Button(self, text = 'SUBMIT', command = self.click_submit_button)
      self.submit.grid(row = 0, column = 2, padx = 20, pady = 20)
    
  def click_submit_button(self):
    
      self.submit_pop_up = submit_button(self.master)
    
      print('New PopUp')

class submit_button(tk.Toplevel):

  def __init__(self, master):
    
      tk.Toplevel.__init__(self, master)
    
      self.master = master
    
      self.title = 'TITLE'

if __name__ == "__main__":

  root = tk.Tk()

  app = Window(root)

  app.pack()

  root.mainloop()

There is something missing from my understanding of the best approach to using OOP to structure a program like this.

0 Answers
Related