如何使用tkinter实现侦听器?

时间:2020-06-04 21:45:03

标签: python tkinter listener

我正在尝试开发一个与tkinter一起使用的对话框,该对话框具有一个输入框和一个按钮。我希望能够在输入框中输入一个值,并在对话框销毁时让输入的值“返回”。以下代码可以正常运行,但是不能如我所描述的那样执行。 GUI上有两个按钮。第一个启动对话框,第二个启动输入的值。我希望消除第二个按钮,并在按下对话框中的“保存输入”按钮时让侦听器激活getValues方法。这是代码:

from tkinter import *

class myDialog():
    def __init__(self):
        self.t = Toplevel()
        self.t.title("Sample")
        self.answer = None
        self.v1 = StringVar()
        self.e1 = Entry(self.t, textvariable=self.v1)
        self.e1.focus()
        self.e1.pack()
        self.saveButton = Button(self.t, text="Save Input", command=self.buttonPressed)
        self.saveButton.pack()

    def buttonPressed(self):
        print("Popup Button Pressed")
        self.answer = self.e1.get()
        self.t.destroy()

class myTk(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.button = Button(text="Display Dialog", command = self.displayPopButton)
        self.button.pack()
        self.getButton = Button(text="Print Values", command=self.getValues)
        self.getButton.pack()

    def displayPopButton(self):
        self.b1 = myDialog()

    def getValues(self):
        print(self.b1.answer)

myTk().mainloop()

2 个答案:

答案 0 :(得分:1)

您可能会在Dialog中将主对象作为参数传递,并在buttonPressed方法内调用master方法:

class myDialog():
    def __init__(self, master):
        self.t = Toplevel()
        self.master = master
        # ... #

    def buttonPressed(self):
        print("Popup Button Pressed")
        self.answer = self.e1.get()
        self.master.getValues()
        self.t.destroy()

class myTk(Tk):
    # ... #
    def displayPopButton(self):
        self.b1 = myDialog(self)

这可以实现您想要的,但是就我个人而言,这不是很好的面向对象操作。它使您的Dialog依赖于具有预期的主类型和所需的方法。您可以将其组织得更清楚一些::

class myDialog():
    def __init__(self, func_to_call):
        self.t = Toplevel()
        self.btn_func = func_to_call
        # ... #

    def buttonPressed(self):
        print("Popup Button Pressed")
        self.answer = self.e1.get()
        func_to_call()
        self.t.destroy()

class myTk(Tk):
    # ... #
    def displayPopButton(self):
        self.b1 = myDialog(self.getValues)

无论如何,我至少将myDialog子类化为Toplevel。也许重新考虑一下我希望对象之间如何相互引用。

答案 1 :(得分:1)

您可以使用myDialoggrab_set()wait_window()设为模式对话框:

from tkinter import *

class myDialog():
    def __init__(self):
        self.t = Toplevel()
        self.t.title("Sample")
        self.answer = None
        self.v1 = StringVar()
        self.e1 = Entry(self.t, textvariable=self.v1)
        self.e1.focus()
        self.e1.pack()
        self.saveButton = Button(self.t, text="Save Input", command=self.buttonPressed)
        self.saveButton.pack()
        # handle user clicking the 'x' button at the title bar
        self.t.protocol('WM_DELETE_WINDOW', self.buttonPressed)

    def buttonPressed(self):
        print("Popup Button Pressed")
        self.answer = self.e1.get()
        self.t.destroy()

    def show(self):
        # make the toplevel act like a modal dialog
        self.t.grab_set()
        self.t.wait_window(self.t)
        # return the answer when the toplevel is closed/destroyed
        return self.answer

class myTk(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.button = Button(text="Display Dialog", command = self.displayPopButton)
        self.button.pack()

    def displayPopButton(self):
        self.b1 = myDialog().show()
        print(self.b1)

myTk().mainloop()