【问题标题】:wxPython wait for dialog response before continuingwxPython 在继续之前等待对话框响应
【发布时间】:2016-04-02 21:26:56
【问题描述】:

我创建了一个对话框,要求用户单击按钮才能继续(例如,“您确定要这样做吗?”)并使用侦听器等待响应

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 变量。

谢谢!!

【问题讨论】:

    标签: python dialog wxpython listener


    【解决方案1】:

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

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

    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...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2020-01-18
      • 2014-07-12
      • 2020-03-11
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多