wxPython 在继续之前等待对话响应

wxPython wait for dialog response before continuing

我创建了一个对话框,要求用户单击按钮以继续(例如 "Are you sure you want to do this?")并使用侦听器等待响应

from wx.lib.pubsub import pub  
...  
pub.subscribe(self.my_listener, "my_listener")

以及点击响应后设置变量的函数。

def my_listener(self, message):
    if message == 'proceed':
        self.proceed = True
    else:
        self.proceed = False

我的代码主体如下所示:

self.proceed = False       # Make sure it's false initially
launch_verify_dialog()     # Launch the dialog
if self.proceed:
    # DO STUFF
else:
    print 'NARF!'

现在,问题是当它运行时,代码会运行 "if self.proceed" if 语句并立即打印 "NARF!",然后我才有机会响应对话框。为什么会发生这种情况,我如何才能在继续之前等待对对话框的响应?

我试过在 if 语句前面放一个循环来等待响应,但这只会使程序崩溃,而且我已经验证了侦听器工作并正确设置了 self.proceed 变量.

谢谢!!

您使用对话框而不是框架并使用 dlg.ShowModal()

或者如果您只是想问一个简单的 yes/no 问题

if wx.MessageBox("Are You Sure?","Checking",wx.YES_NO) == wx.YES:
   print ("User Clicked Yes")
else:
   print ("User did not click yes (clicked No or closed)")

如果你需要更复杂的东西

class MyDialog(wx.Dialog):
    def __init__(self):
       wx.Dialog.__init__(self,None,-1,"A Title")
       wx.StaticText(self,-1,"Some Text!")
       b = wx.Button(self,-1,"Click",pos=(100,100))
       b.Bind(wx.EVT_BUTTON, self.OnClick)
     def OnClick(self,evt):
       pub.sendMessage("whatever")
       self.Destroy()

 MyDialog().ShowModal()
 #will not continue until MyDialog Closes...