【发布时间】:2021-06-25 22:40:48
【问题描述】:
我几乎是一个 Python 初学者,最近才开始使用 wxpython,现在有以下问题:
我正在编写一个应用程序,其中一个窗口打开,通过单击一个按钮,用户打开第二个窗口,他们应该在其中输入他们的姓名并单击另一个按钮。我的问题是我不知道如何暂停通过单击 Button1 执行的“事件代码”,直到用户在第二个窗口中完成他们的操作。
有没有办法暂停 OnButton1 中的代码(在 secondframe.Show() 行之后,直到用户在 SecondFrame 中输入他们的姓名并点击 OnButton2?
基本上我的目标是首先打印用户名,然后才打印完成
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
self.Button1 = wx.Button(self.panel, label='Button 1', pos=(20, 10), size=(90, 25))
#Set event handlers
self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1)
def OnButton1(self, e):
secondframe = SecondFrame(None)
secondframe.Show()
print("Done")
class SecondFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
self.panel = wx.Panel(self)
self.Button2 = wx.Button(self.panel, label='Button 2', pos=(50, 20), size=(90, 25))
self.NameBox = wx.TextCtrl(self.panel, value="Type Your Name", pos=(50, 60), size=(200, -1))
#Set event handlers
self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2)
def OnButton2(self, e):
Name = self.NameBox.GetValue()
print(Name)
app = wx.App(False)
frame = MainFrame(None)
frame.Show()
app.MainLoop()
【问题讨论】:
-
常用对话框类和函数封装了常用的对话框需求。它们都是“模态的”,在用户关闭对话框之前获取控制流,使它们在应用程序中易于使用。
标签: python pycharm event-handling wxpython frame