如何用tkinter实现监听器?

问题描述 投票:0回答:2

我想用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()
python tkinter listener
2个回答
1
投票

你可以在Dialog中传递你的主对象作为param,然后在Dialog中调用主方法。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. 也许还要重新思考我希望这些对象如何相互参照。


1
投票

你可以让 myDialog 模态对话框,使用 grab_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()
© www.soinside.com 2019 - 2024. All rights reserved.