首页 > 解决方案 > 如何使用 tkinter 实现监听器?

问题描述

我正在尝试开发一个与 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()

标签: pythontkinterlistener

解决方案


您可以在对话框中将主对象作为参数传递,并在方法中调用主buttonPressed方法:

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)

这实现了你想要的,但我个人认为这不是好的 OOP。它使您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. 也许重新考虑我希望对象如何相互引用。


推荐阅读