是否可以为python的tkinter中的每个类保持相同的窗口?

时间:2016-01-05 11:52:29

标签: python class tkinter window root

我正在尝试在tkinter中创建一个程序,它允许我打开一个初始窗口,然后在所有使用的类中保留它。例如,如果我要在窗口中创建一个按钮,那么当我单击此按钮时,它将执行一个销毁窗口小部件的方法,然后执行在同一窗口中构建新屏幕的新类,例如反对的文本按钮。

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()

我明白这不太实际,但我很好奇。欢呼声。

2 个答案:

答案 0 :(得分:0)

Tk()创建主窗口,变量root可让您访问此窗口。您可以使用root作为Window2的参数,然后您就可以访问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()

-

可能没有人使用其他类来代替Label来创建Button;)

-

编辑:在此示例中,使用名称Window1Windows2会产生误导,因为只有一个窗口和两个使用此窗口的类。我宁愿使用名称FirstOwnerSecondOwner

答案 1 :(得分:0)

一切都在一个Tk类中实现,在这种情况下,总是只有一个窗口。

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()