wxpython-如何从自定义 wx 对话框中获取 return 值
wxpython-how to get return value from custom wx dialog
这是
的跟进问题
我创建了自定义 dialog.It 当我们从另一个函数调用一个函数时会弹出,如果用户单击对话框上的 "Dont proceed" 按钮 window 我需要得到 true/False 提示特定按钮是否被按下的值,并将该值传递给函数调用以继续进行。
但是对于 wx 自定义对话框,我无法获得任何 return 值,因为一旦我们按下任何按钮,对话框就会被破坏。
有没有什么方法可以通过按钮单击事件从自定义对话框 class 中获取 return 值,并且可以从 class 外部访问,即使它已被销毁。
提前致谢。
def main():
print "start execution"
ret = getUserInput("Do you want to proceed?")
if ret:
print "proceed"
else:
print "exit"
def getUserInput(msg):
class Busy(wx.Dialog):
def __init__(self, parent, msg):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Message", size=(420, 200))
self.panel = wx.Panel(self, wx.ID_ANY)
self.label = wx.StaticText(self.panel, label=msg, pos=(20, 20))
self.gauge = wx.Gauge(self.panel, size=(300, 20), pos=(50, 50), style=wx.GA_HORIZONTAL)
self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(50, 80))
self.lltime = wx.StaticText(self.panel, label="30", pos=(130, 80))
self.notProceedButton = wx.Button(self.panel, label="Don't proceed", pos=(50, 100))
self.timeoutButton = wx.Button(self.panel, label="Timer Off", pos=(250, 100))
self.notProceedButton.Bind(wx.EVT_BUTTON, self.notProceed)
self.timeoutButton.Bind(wx.EVT_BUTTON, self.OnNoTimeout)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.lifetimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnLifeTimer, self.lifetimer)
self.timer.Start(100)
self.lifetimer.Start(1000)
self.timeoutbutton_pressed = False
self.gauge.SetBackgroundColour(wx.Colour(0, 127, 255, 255)) # Slate Blue
self.gauge.SetRange(100)
self.gauge.SetValue(0)
self.life = 30
self.direction = 1
self.result_text = True
def OnTimer(self, evt): # Update gauge
x = int(self.gauge.GetValue())
if x == 0:
self.direction = 1
elif x == 100:
self.direction = -1
x += self.direction
self.gauge.SetValue(x)
def OnLifeTimer(self, evt): # Update time to live
if self.timeoutbutton_pressed == True:
return
self.life -= 1
self.lltime.SetLabelText(str(self.life))
if self.life < 1:
self.OnQuit(None)
def OnNoTimeout(self, evt): # toggle time to live
if self.timeoutbutton_pressed == False:
self.timeoutbutton_pressed = True
self.timeoutButton.SetLabel("Timer On")
else:
self.timeoutbutton_pressed = False
self.timeoutButton.SetLabel("Timer Off")
def OnQuit(self, event):
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
def notProceed(self, event): # return input
self.result_text = False
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
app = wx.App()
dlg = Busy(parent = None, msg=msg)
dlg.ShowModal()
return dlg.result_text
当然可以。 EndModal
方法将 return 任何你想要的 return 代码。即
def OnLifeTimer(self, evt): # Update time to live
if self.timeoutbutton_pressed == True:
return
self.life -= 1
self.lltime.SetLabelText(str(self.life))
if self.life < 1:
self.EndModal(SOME_ID_THAT_YOU_DEFINED_SOMEWHERE_ELSE)
self.OnQuit(None)
传递给 EndModal
的参数是 return 来自 ShowModal
app = wx.App()
dlg = Busy(parent = None, msg=msg)
return_code = dlg.ShowModal()
附带说明一下,您不必从对话框中调用 Destroy。如果你想从中检索一些标志或其他信息,你可以在之后调用 Destroy
,即
app = wx.App()
dlg = Busy(parent = None, msg=msg)
return_value = dlg.ShowModal()
some_other_value = dlg.some_other_value
dlg.Destroy()
感谢 user2682863 的优秀范例 EndModal()
import wx
class getUserInput(wx.Dialog):
def __init__(self, parent, msg):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Message", size=(420, 200))
self.panel = wx.Panel(self, wx.ID_ANY)
self.label = wx.StaticText(self.panel, label=msg, pos=(20, 20))
self.gauge = wx.Gauge(self.panel, size=(300, 20), pos=(50, 50), style=wx.GA_HORIZONTAL)
self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(50, 80))
self.lltime = wx.StaticText(self.panel, label="30", pos=(130, 80))
self.notProceedButton = wx.Button(self.panel, label="Don't proceed", pos=(50, 100))
self.timeoutButton = wx.Button(self.panel, label="Timer Off", pos=(250, 100))
self.notProceedButton.Bind(wx.EVT_BUTTON, self.notProceed)
self.timeoutButton.Bind(wx.EVT_BUTTON, self.OnNoTimeout)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.lifetimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnLifeTimer, self.lifetimer)
self.timer.Start(100)
self.lifetimer.Start(1000)
self.timeoutbutton_pressed = False
self.gauge.SetBackgroundColour(wx.Colour(0, 127, 255, 255)) # Slate Blue
self.gauge.SetRange(100)
self.gauge.SetValue(0)
self.life = 30
self.direction = 1
self.result_text = True
def OnTimer(self, evt): # Update gauge
x = int(self.gauge.GetValue())
if x == 0:
self.direction = 1
elif x == 100:
self.direction = -1
x += self.direction
self.gauge.SetValue(x)
def OnLifeTimer(self, evt): # Update time to live
if self.timeoutbutton_pressed == True:
return
self.life -= 1
self.lltime.SetLabelText(str(self.life))
if self.life < 1:
self.OnQuit(None)
def OnNoTimeout(self, evt): # toggle time to live
if self.timeoutbutton_pressed == False:
self.timeoutbutton_pressed = True
self.timeoutButton.SetLabel("Timer On")
else:
self.timeoutbutton_pressed = False
self.timeoutButton.SetLabel("Timer Off")
def OnQuit(self, event):
self.timer.Stop()
self.lifetimer.Stop()
self.EndModal(True)
def notProceed(self, event): # return input
self.result_text = False
self.timer.Stop()
self.lifetimer.Stop()
self.EndModal(False)
app = wx.App()
dlg = getUserInput(parent = None, msg="Do you want to porceed?")
ret = dlg.ShowModal()
if ret:
print "proceed"
else:
print "exit"
这是
我创建了自定义 dialog.It 当我们从另一个函数调用一个函数时会弹出,如果用户单击对话框上的 "Dont proceed" 按钮 window 我需要得到 true/False 提示特定按钮是否被按下的值,并将该值传递给函数调用以继续进行。
但是对于 wx 自定义对话框,我无法获得任何 return 值,因为一旦我们按下任何按钮,对话框就会被破坏。
有没有什么方法可以通过按钮单击事件从自定义对话框 class 中获取 return 值,并且可以从 class 外部访问,即使它已被销毁。
提前致谢。
def main():
print "start execution"
ret = getUserInput("Do you want to proceed?")
if ret:
print "proceed"
else:
print "exit"
def getUserInput(msg):
class Busy(wx.Dialog):
def __init__(self, parent, msg):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Message", size=(420, 200))
self.panel = wx.Panel(self, wx.ID_ANY)
self.label = wx.StaticText(self.panel, label=msg, pos=(20, 20))
self.gauge = wx.Gauge(self.panel, size=(300, 20), pos=(50, 50), style=wx.GA_HORIZONTAL)
self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(50, 80))
self.lltime = wx.StaticText(self.panel, label="30", pos=(130, 80))
self.notProceedButton = wx.Button(self.panel, label="Don't proceed", pos=(50, 100))
self.timeoutButton = wx.Button(self.panel, label="Timer Off", pos=(250, 100))
self.notProceedButton.Bind(wx.EVT_BUTTON, self.notProceed)
self.timeoutButton.Bind(wx.EVT_BUTTON, self.OnNoTimeout)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.lifetimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnLifeTimer, self.lifetimer)
self.timer.Start(100)
self.lifetimer.Start(1000)
self.timeoutbutton_pressed = False
self.gauge.SetBackgroundColour(wx.Colour(0, 127, 255, 255)) # Slate Blue
self.gauge.SetRange(100)
self.gauge.SetValue(0)
self.life = 30
self.direction = 1
self.result_text = True
def OnTimer(self, evt): # Update gauge
x = int(self.gauge.GetValue())
if x == 0:
self.direction = 1
elif x == 100:
self.direction = -1
x += self.direction
self.gauge.SetValue(x)
def OnLifeTimer(self, evt): # Update time to live
if self.timeoutbutton_pressed == True:
return
self.life -= 1
self.lltime.SetLabelText(str(self.life))
if self.life < 1:
self.OnQuit(None)
def OnNoTimeout(self, evt): # toggle time to live
if self.timeoutbutton_pressed == False:
self.timeoutbutton_pressed = True
self.timeoutButton.SetLabel("Timer On")
else:
self.timeoutbutton_pressed = False
self.timeoutButton.SetLabel("Timer Off")
def OnQuit(self, event):
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
def notProceed(self, event): # return input
self.result_text = False
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
app = wx.App()
dlg = Busy(parent = None, msg=msg)
dlg.ShowModal()
return dlg.result_text
当然可以。 EndModal
方法将 return 任何你想要的 return 代码。即
def OnLifeTimer(self, evt): # Update time to live
if self.timeoutbutton_pressed == True:
return
self.life -= 1
self.lltime.SetLabelText(str(self.life))
if self.life < 1:
self.EndModal(SOME_ID_THAT_YOU_DEFINED_SOMEWHERE_ELSE)
self.OnQuit(None)
传递给 EndModal
的参数是 return 来自 ShowModal
app = wx.App()
dlg = Busy(parent = None, msg=msg)
return_code = dlg.ShowModal()
附带说明一下,您不必从对话框中调用 Destroy。如果你想从中检索一些标志或其他信息,你可以在之后调用 Destroy
,即
app = wx.App()
dlg = Busy(parent = None, msg=msg)
return_value = dlg.ShowModal()
some_other_value = dlg.some_other_value
dlg.Destroy()
感谢 user2682863 的优秀范例 EndModal()
import wx
class getUserInput(wx.Dialog):
def __init__(self, parent, msg):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Message", size=(420, 200))
self.panel = wx.Panel(self, wx.ID_ANY)
self.label = wx.StaticText(self.panel, label=msg, pos=(20, 20))
self.gauge = wx.Gauge(self.panel, size=(300, 20), pos=(50, 50), style=wx.GA_HORIZONTAL)
self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(50, 80))
self.lltime = wx.StaticText(self.panel, label="30", pos=(130, 80))
self.notProceedButton = wx.Button(self.panel, label="Don't proceed", pos=(50, 100))
self.timeoutButton = wx.Button(self.panel, label="Timer Off", pos=(250, 100))
self.notProceedButton.Bind(wx.EVT_BUTTON, self.notProceed)
self.timeoutButton.Bind(wx.EVT_BUTTON, self.OnNoTimeout)
self.Bind(wx.EVT_CLOSE, self.OnQuit)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.lifetimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnLifeTimer, self.lifetimer)
self.timer.Start(100)
self.lifetimer.Start(1000)
self.timeoutbutton_pressed = False
self.gauge.SetBackgroundColour(wx.Colour(0, 127, 255, 255)) # Slate Blue
self.gauge.SetRange(100)
self.gauge.SetValue(0)
self.life = 30
self.direction = 1
self.result_text = True
def OnTimer(self, evt): # Update gauge
x = int(self.gauge.GetValue())
if x == 0:
self.direction = 1
elif x == 100:
self.direction = -1
x += self.direction
self.gauge.SetValue(x)
def OnLifeTimer(self, evt): # Update time to live
if self.timeoutbutton_pressed == True:
return
self.life -= 1
self.lltime.SetLabelText(str(self.life))
if self.life < 1:
self.OnQuit(None)
def OnNoTimeout(self, evt): # toggle time to live
if self.timeoutbutton_pressed == False:
self.timeoutbutton_pressed = True
self.timeoutButton.SetLabel("Timer On")
else:
self.timeoutbutton_pressed = False
self.timeoutButton.SetLabel("Timer Off")
def OnQuit(self, event):
self.timer.Stop()
self.lifetimer.Stop()
self.EndModal(True)
def notProceed(self, event): # return input
self.result_text = False
self.timer.Stop()
self.lifetimer.Stop()
self.EndModal(False)
app = wx.App()
dlg = getUserInput(parent = None, msg="Do you want to porceed?")
ret = dlg.ShowModal()
if ret:
print "proceed"
else:
print "exit"