python - 如何使用 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()
解决方案
您可以在对话框中将主对象作为参数传递,并在方法中调用主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
. 也许重新考虑我希望对象如何相互引用。
推荐阅读
- javascript - 错误:反应本机应用程序内的 html 文本内的 Youtube 链接
- java - 如何使用 Java 找到 2 个像素图像之间的相似性
- constraints - 系统verilog中约束中的多个foreach语句使用
- google-analytics - 使用 Google 跟踪代码管理器将站点速度采样率提高到 100,但对于有访问者的页面仍然显示为空
- java - 如何更改 ResourceServerConfiguration 顺序
- excel - 在excel vba中将数据转换为数据值代码
- angular - 如何在 Angular 中关闭 ZoneAwareError?
- javascript - 当我从 Xcode 运行时,React Native 项目无法加载
- ruby-on-rails - 如何根据特定日期范围对记录进行分组?
- javascript - 为数组中的每个项目绑定一个后缀