Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Tutorials listed in this section have not been reviewed nor are affliated by Code Forum. It is the sole responsbility of the authour to properly maintain and ensure information is accurate.

Python tkinter working with toplevel window

Example of tkinter's toplevel window using the mvc (Model, View, Controller) approach

Python:
# Do the imports
import tkinter as tk

class TopWindow:
    ''' Class for a Toplevel window '''
    def __init__(self):
        ''' Initiate the window '''
        self.window = tk.Toplevel(None)
        self.window.title('Toplevel Window')
        self.window.geometry('400x200+300+300')
        self.window['bg'] = 'tan'

        # Create the label and a button
        label= tk.Label(self.window, text='This is a toplevel window', bg='tan')
        label.pack(side='top')

        self.button = tk.Button(self.window, text='Close')
        self.button.pack(side='bottom', padx=10, pady=10)


class MainWindow:
    ''' The main window view '''
    def __init__(self, parent):
        self.parent = parent
        parent.geometry('400x200+300+300')
        parent['bg'] = 'gold'
        parent.title('Main Window')

        # Create the label and a couple of buttons
        label = tk.Label(parent, text='This is the main window', bg='gold')
        label.pack(side='top')

        self.button = tk.Button(parent, text='Open Window')
        self.button.pack(side='left', padx=10, pady=10)

        self.close_button = tk.Button(parent, text='Close')
        self.close_button.pack(side='left')


class Controller:
    ''' Controller class does the communictaions between the windows '''
    def __init__(self, mainwindow, topwindow):
        self.mainwindow = mainwindow
        self.topwindow = topwindow

        # We call tkinters withdraw function to keep the toplevel window from displaying.
        # There are other ways of doing this, I find this the easiest most of the time
        self.topwindow.window.withdraw()

        # Set button commands for the main window and toplevel window
        self.mainwindow.button['command'] = self.openwindow
        self.mainwindow.close_button['command'] = self.mainwindow.parent.destroy
        self.topwindow.button['command'] = self.closewindow

        # Command for the titlebar close on the toplevel window
        self.topwindow.window.protocol('WM_DELETE_WINDOW', self.closewindow)
      
    def openwindow(self):
        ''' Method for openeing the toplevel window and withdrawing the main window '''
        self.topwindow.window.deiconify()
        self.mainwindow.parent.withdraw()

    def closewindow(self):
        ''' Method for withdrawing toplevel window and deiconifying main window '''
        self.topwindow.window.withdraw()
        self.mainwindow.parent.deiconify()


if __name__ == '__main__':
    root = tk.Tk()
    controller = Controller(MainWindow(root), TopWindow())
    root.mainloop()
 

New Threads

Buy us a coffee!

Back
Top Bottom