【发布时间】:2015-10-15 20:49:15
【问题描述】:
在过去的一个小时里,我一直试图寻找这个问题的答案,但我没有找到任何东西。 (我觉得这应该在互联网上)。
当您单击父 GUI 框架上的按钮时,我正在尝试制作自定义弹出窗口。然后我希望用户输入变量,然后将这些变量返回给原始类。我基本上是在弹出一个输入选项。但是,我应该注意,并非所有选项都必须输入,这取决于用户的情况。用户也可以随时单击“选项”按钮,因此当弹出窗口退出时,我希望它在最初点击选项按钮时返回到父 GUI。我正在做的事情如下:
from Tkinter import *
class Home(Frame): #This class defines the main(master) GUI
def __init__(self, master):
Frame.__init__(self, master)
#Other methods in class "Home" that get, variables, etc.
def call_popup(self):
options_popup = Options(self)
#wait_window does not work here
self.master.wait_window(options_popup)
class Options(Toplevel): #This class defines the popup GUI
def __init__(self, master):
Toplevel.__init__(self, master)
self.popup = self
#Other methods in class "Options"
def get_variable(self): #Just an example methods in class "Options"
#Simplified code but lets say I now have the variable foo in self.foo, unique to class "Options"
#I want to pass this variable back to class "Home"
def exit_popup(self):
self.popup.destroy()
#I want it to return to the spot that the user was originally at
#when they hit the "options button" in the home GUI. I also want all
#values inputted in the "options" popup to return to the class "Home"
root = Tk()
Home(root)
root.mainloop()
我认为问题的一个(很大)部分是我对类(以及 Tkinter)非常陌生。
我查看了很多关于 tkinter、弹出窗口、类继承和 wait_window 的问题,但遗憾的是我仍然无法弄清楚。
我也尝试过像这样传递“选项”类:def __init__(self, master, options):(第 3 行),但这超出了参数的数量。
所以,主要问题: -如何将变量从弹出窗口/类“选项”传递到“主页”类? -你如何让弹出/类“选项”窗口等到关闭才能恢复到“主页”类?
【问题讨论】: