显示限时消息wxPython
Show a limited time Message wxPython
我在 wxPython 中构建了一个应用程序,在该应用程序中我想在有限的时间内显示一条消息,然后它会自行消失(应用程序的用户不需要任何东西来让它消失)
我试过那样做,但它没有自己关闭。
dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.Destroy).start()
dialog.ShowModal()
我也试过这样做,但它什么也没做:
dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.EndModal,args=wx.ID_OK).start()
dialog.ShowModal()
我没有足够的上下文来了解你的计时器有什么问题。不过你可以试试这个。
import wx
# ================================================================================
class TimedDialog(wx.Dialog):
def __init__(self, *args, **kwargs):
super(TimedDialog, self).__init__(*args, **kwargs)
self.SetSize((400, 300))
self.SetTitle('Please wait!')
self.Centre()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(2000) # 2 second interval
def OnTimer(self, event):
self.Close()
# ================================================================================
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.SetSize((300, 200))
self.SetTitle('app')
self.Centre()
self.btn = wx.Button(self, -1, "click Me")
self.btn.Bind(wx.EVT_BUTTON, self.OnClicked)
def OnClicked(self, event):
dlg = TimedDialog(self)
dlg.ShowModal()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
我使用 dialog.DestroyLater
让你的对话框关闭,但它并不一致,从准时到迟到 20 秒不等。几乎没有你想要的。
这是我 2 年前放在一起的东西,但现在不记得为什么了。
有点过头了,希望对你有帮助。
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Busy Dialog",size=(500,200))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(400,100),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
self.button = wx.Button(self.panel, label="Click me")
sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self,event):
dlg = Busy(parent = self.panel)
dlg.ShowModal()
if dlg.result_text:
self.log.AppendText("Text Input: "+dlg.result_text+"\n")
dlg.Destroy()
class Busy(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Busy", size= (420,240))
self.panel = wx.Panel(self,wx.ID_ANY)
self.label = wx.StaticText(self.panel, label="Input", pos=(20,20))
self.textinput = wx.TextCtrl(self.panel, value="", pos=(80,20), size=(300,-1))
self.gauge = wx.Gauge(self.panel,size=(300,20),pos=(80,80), style=wx.GA_HORIZONTAL)
self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(80,110))
self.lltime = wx.StaticText(self.panel, label="30", pos=(160,110))
self.saveButton =wx.Button(self.panel, label="Save Input", pos=(80,160))
self.closeButton =wx.Button(self.panel, label="Cancel", pos=(180,160))
self.timeoutButton =wx.Button(self.panel, label="Timer Off", pos=(280,160))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveBusyString)
self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
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 = None
self.Show()
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.result_text = self.textinput.GetValue()
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 SaveBusyString(self, event): # return input
self.result_text = self.textinput.GetValue()
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
我在 wxPython 中构建了一个应用程序,在该应用程序中我想在有限的时间内显示一条消息,然后它会自行消失(应用程序的用户不需要任何东西来让它消失) 我试过那样做,但它没有自己关闭。
dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.Destroy).start()
dialog.ShowModal()
我也试过这样做,但它什么也没做:
dialog = wx.MessageDialog(None,'message', wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
threading.Timer(2.0, dialog.EndModal,args=wx.ID_OK).start()
dialog.ShowModal()
我没有足够的上下文来了解你的计时器有什么问题。不过你可以试试这个。
import wx
# ================================================================================
class TimedDialog(wx.Dialog):
def __init__(self, *args, **kwargs):
super(TimedDialog, self).__init__(*args, **kwargs)
self.SetSize((400, 300))
self.SetTitle('Please wait!')
self.Centre()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(2000) # 2 second interval
def OnTimer(self, event):
self.Close()
# ================================================================================
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.SetSize((300, 200))
self.SetTitle('app')
self.Centre()
self.btn = wx.Button(self, -1, "click Me")
self.btn.Bind(wx.EVT_BUTTON, self.OnClicked)
def OnClicked(self, event):
dlg = TimedDialog(self)
dlg.ShowModal()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
我使用 dialog.DestroyLater
让你的对话框关闭,但它并不一致,从准时到迟到 20 秒不等。几乎没有你想要的。
这是我 2 年前放在一起的东西,但现在不记得为什么了。
有点过头了,希望对你有帮助。
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Busy Dialog",size=(500,200))
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(400,100),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
self.button = wx.Button(self.panel, label="Click me")
sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self,event):
dlg = Busy(parent = self.panel)
dlg.ShowModal()
if dlg.result_text:
self.log.AppendText("Text Input: "+dlg.result_text+"\n")
dlg.Destroy()
class Busy(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Busy", size= (420,240))
self.panel = wx.Panel(self,wx.ID_ANY)
self.label = wx.StaticText(self.panel, label="Input", pos=(20,20))
self.textinput = wx.TextCtrl(self.panel, value="", pos=(80,20), size=(300,-1))
self.gauge = wx.Gauge(self.panel,size=(300,20),pos=(80,80), style=wx.GA_HORIZONTAL)
self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(80,110))
self.lltime = wx.StaticText(self.panel, label="30", pos=(160,110))
self.saveButton =wx.Button(self.panel, label="Save Input", pos=(80,160))
self.closeButton =wx.Button(self.panel, label="Cancel", pos=(180,160))
self.timeoutButton =wx.Button(self.panel, label="Timer Off", pos=(280,160))
self.saveButton.Bind(wx.EVT_BUTTON, self.SaveBusyString)
self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
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 = None
self.Show()
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.result_text = self.textinput.GetValue()
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 SaveBusyString(self, event): # return input
self.result_text = self.textinput.GetValue()
self.timer.Stop()
self.lifetimer.Stop()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()