python对象之间的通讯?

时间:2018-10-08 16:30:13

标签: python oop tkinter

对象之间的通讯

想法是从Gui中创建一个Toplevel窗口,并在Toplevel关闭后将数据(名称)从Toplevel Entry发送回Gui

对象应用程序如何知道toplev对象是否被破坏?

或换句话说

Gui的对象如何知道My_Toplevel的对象已关闭?

from tkinter import *
font1 = font=("Open Sans Standard",16,"bold")

class My_Toplevel():
    def __init__(self, master=None):
        self.master = master
        self.toplev = Toplevel(master)
        self.name = None
        self.create_widgets()
    def create_widgets(self):
        self.entry_name = Entry(self.toplev, font=font1)
        self.button_ok = Button(self.toplev, text="Ok", font=font1,
                                command=self.get_name)
        self.entry_name.pack()
        self.button_ok.pack()
    def get_name(self):
        self.name = self.entry_name.get()
        self.toplev.destroy()

class Gui(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.pack()
        self.master = master
        self.label_text = Label(self, text="Foo Bar Window", font=font1)
        self.label_text.pack()
        self.button_toplevel = Button(self, text="Create Toplevel", 
                                command=self.get_toplevel, font=font1)
        self.button_toplevel.pack()

    def get_toplevel(self):
        self.my_top = My_Toplevel(self)

if __name__ == "__main__":
    root = Tk()
    root.title("Parent")
    app = Gui(root)
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

在销毁Gui之前,需要将数据传递到My_Toplevel实例。一种实现方法是将名称字符串另存为Gui实例的属性,因为在调用master时传递了My_Toplevel参数。例如:

from tkinter import *
font1 = font=("Open Sans Standard",16,"bold")

class My_Toplevel():
    def __init__(self, master=None):
        self.master = master
        self.toplev = Toplevel(master)
        self.create_widgets()

    def create_widgets(self):
        self.entry_name = Entry(self.toplev, font=font1)
        self.button_ok = Button(self.toplev, text="Ok", font=font1,
                                command=self.get_name)
        self.entry_name.pack()
        self.button_ok.pack()

    def get_name(self):
        self.master.name_data = self.entry_name.get()
        self.toplev.destroy()

class Gui(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.pack()
        self.master = master
        self.label_text = Label(self, text="Foo Bar Window", font=font1)
        self.label_text.pack()
        self.button_toplevel = Button(self, text="Create Toplevel", 
                                command=self.get_toplevel, font=font1)
        self.button_toplevel.pack()

        self.name_data = None
        Button(self, text="show name", command=self.show_name).pack()

    def show_name(self):
        print("Name =", self.name_data)

    def get_toplevel(self):
        self.my_top = My_Toplevel(self)

if __name__ == "__main__":
    root = Tk()
    root.title("Parent")
    app = Gui(root)
    root.mainloop()

按下“显示名称”按钮,将名称字符串打印到控制台。

如果您需要保存多个字符串,可以将名称附加到列表中,将其保存在字典中,等等。

如果愿意,可以在销毁TopLevel窗口之前调用Gui.show_name方法:

def get_name(self):
    self.master.name_data = self.entry_name.get()
    self.master.show_name()
    self.toplev.destroy()