0

I am trying to create a program in tkinter which allows me to open an initial window then to keep it throughout all classes used. For example, if I was to create a button in a window then when I click this button, it would exuecute a method that destroys the widget, and then executes a new class that builds a new screen within the same window, such as text opposed to a button.

from tkinter import *

class Window1:

    def __init__(self, master):
        self.master = master
        self.label = Button(self.master, text = "Example", command = self.load_new)
        self.label.pack()

    def load_new(self):
        self.label.destroy()
        ## Code to execute next class

class Window2:

    def __init__(self, master):
        self.master = master
        self.label = Label(self.master, text = "Example")
        self.label.pack()

def main():
    root = Tk()
    run = Window1(root)
    root.mainloop()

if __name__ == '__main__':
main()

I understand this is less practical, but I am curious. Cheers.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Maximillion Bartango
  • 1,533
  • 1
  • 19
  • 34
  • If you just want additional windows, you can use the `Toplevel` widget. If you want to hide the root object, you can use the `withdraw()` method. – TigerhawkT3 Jan 05 '16 at 12:19
  • `Tk()` creates main window and `root` give you access to this window. You can use `root` as argument for `Window2` and has access to main window inside `Window2` - but probably nobody use another class to create `Label` in place of `Button`. – furas Jan 05 '16 at 12:37
  • Does the following answer help? http://stackoverflow.com/a/7557028/7432 – Bryan Oakley Jan 05 '16 at 13:27
  • Single window applications with changing contents are common. As furas said, your names `Window1` and `Window2` are misleading. `Stage` or `Display` or ??? or 'Intro` and `Main` would be better.. If both are subclasses of Frame, then `Frame1` and `Frame2` would be good. – Terry Jan Reedy Jan 06 '16 at 02:39

2 Answers2

0

Tk() creates main window and variable root gives you access to this window. You can use root as argument for Window2 and you will have access to main window inside Window2

from tkinter import *


class Window1:

    def __init__(self, master):

        # keep `root` in `self.master`
        self.master = master 

        self.label = Button(self.master, text="Example", command=self.load_new)
        self.label.pack()

    def load_new(self):
        self.label.destroy()

        # use `root` with another class
        self.another = Window2(self.master)


class Window2:

    def __init__(self, master):

        # keep `root` in `self.master`
        self.master = master

        self.label = Label(self.master, text="Example")
        self.label.pack()


root = Tk()
run = Window1(root)
root.mainloop()

--

Probably nobody use another class to create Label in place of Button ;)

--

EDIT: In this example using names Window1 and Windows2 is misleading because there is only one window and two classes which use this window. I would rather use names FirstOwner, SecondOwner

furas
  • 134,197
  • 12
  • 106
  • 148
0

Everything is implemented in one Tk class and in this case there always is only one window.

from tkinter import *
from tkinter import ttk


class MainWindow():

    def __init__(self, mainWidget):
        self.main_frame = ttk.Frame(mainWidget, width=300, height=150, padding=(0, 0, 0, 0))
        self.main_frame.grid(row=0, column=0)

        self.some_kind_of_controler = 0

        self.main_gui()

    def main_gui(self):
        root.title('My Window')

        self.main_label_1 = ttk.Label(self.main_frame, text='Object_1')
        self.main_label_1.grid(row=0, column=0)

        self.main_label_2 = ttk.Label(self.main_frame, text='Object_2')
        self.main_label_2.grid(row=1, column=0)

        self.main_label_3 = ttk.Label(self.main_frame, text='Object_3')
        self.main_label_3.grid(row=2, column=0)

        self.setings_button = ttk.Button(self.main_frame, text='Setings')
        self.setings_button.grid(row=0, column=1)
        self.setings_button.bind('<Button-1>', self.setings_gui)

        self.gui_elements = [self.main_label_1,
                             self.main_label_2,
                             self.main_label_3,
                             self.setings_button]

    def setings_gui(self, event):
        self.gui_elements_remove(self.gui_elements)

        root.title('Setings')

        self.main_label_1 = ttk.Label(self.main_frame, text='Object_1')
        self.main_label_1.grid(row=2, column=0)

        self.main_menu_button = ttk.Button(self.main_frame, text='Main menu')
        self.main_menu_button.grid(row=0, column=1)
        self.main_menu_button.bind('<Button-1>', self.back_to_main)

        self.some_kind_of_controler = 1

        self.gui_elements = [self.main_label_1,
                             self.main_menu_button]

    def back_to_main(self, event):
        if self.some_kind_of_controler == 1:
            self.gui_elements_remove(self.gui_elements)
        else:
            pass
        self.main_gui()

    def gui_elements_remove(self, elements):
        for element in elements:
            element.destroy()

def main():
    global root

    root = Tk()
    root.geometry('300x150+50+50')
    window = MainWindow(root)

    root.mainloop()

if __name__ == '__main__':
    main()
frankot
  • 190
  • 2
  • 3
  • 14