繁体   English   中英

如何使用 tkinter 实现监听器?

[英]How to implement listener using 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()

您可以在对话框中将主 object 作为参数传递,并在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 也许重新考虑我希望对象如何相互引用。

您可以使用grab_set()wait_window()使myDialog成为模态对话框:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM