How to execute the function in a tkinter sub window by the tkinter main window

88 Views Asked by At

In Tkinter, how can I execute a function in a subwindow (Toplevel window) from the main window? In the code below, after I click the sayHelloInNewWindow button from the main window, I expected it to print a label that say Hello in a new window.

import ttkbootstrap as tb
import tkinter as tk

class Main():
    def __init__(self, window):
        # Window
        window.title("Main") # Set the window title
        window.geometry("480x720") # Set window size  

        createWindowButton = tb.Button(window, text="Create a window", command=self.new_window)
        createWindowButton.pack(side="top")

        sayHelloInNewWindow = tb.Button(window, text="Say Hello in New Window", command=self.newtab.sayhello)
        sayHelloInNewWindow.pack(side="bottom")

    def new_window(self):
        self.newtab = NewWindow(tk.Toplevel(window))


class NewWindow():
    def __init__(self, window):
        window.title("New Window")
        window.geometry("480x720") # Set window size  
    def sayhello(self):
        helloLabel = tb.Label(window, text="Hello")
        helloLabel.pack(side="top")

if __name__ == "__main__":  # only runs when this file is run as a standalone
    theme = "cyborg"
    window = tb.Window(themename=theme)        # create a TK object
    Main(window)     # open the VideoPlayer GUI
    window.mainloop()       # run the window main loop, reacting to button presses, etc
1

There are 1 best solutions below

0
On

It is better that Main inherits from tb.Window and NewWindow inherits from tb.Toplevel. Also you need to check whether the self.newtab is created if you want to call its function sayhello().

Below is the modified code:

import ttkbootstrap as tb

class Main(tb.Window):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.title("Main") # Set the window title
        self.geometry("480x720") # Set window size

        # initialize self.newtab
        self.newtab = None

        createWindowButton = tb.Button(self, text="Create a window", command=self.new_window)
        createWindowButton.pack(side="top")

        sayHelloInNewWindow = tb.Button(self, text="Say Hello in New Window", command=self.newtab_sayhello)
        sayHelloInNewWindow.pack(side="bottom")

    def newtab_sayhello(self):
        if self.newtab and self.newtab.winfo_exists():
            # the window is open, so call its sayhello()
            self.newtab.sayhello()

    def new_window(self):
        if self.newtab is None or not self.newtab.winfo_exists():
            # the window is not created or closed
            # so create new one
            self.newtab = NewWindow(self)
        else:
            # the window is already open
            # so bring it to front
            self.newtab.lift()


class NewWindow(tb.Toplevel):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.title("New Window")
        self.geometry("480x720") # Set window size

    def sayhello(self):
        helloLabel = tb.Label(self, text="Hello")
        helloLabel.pack(side="top")

if __name__ == "__main__":  # only runs when this file is run as a standalone
    theme = "cyborg"
    window = Main(themename=theme)
    window.mainloop()