woggy wrote:I need to abort this script if there is no input for 10 seconds. I have tried a lot of different things but I'm no programmer =)
After some time I thought of your problem.
You can try this script:
Code: Select all
from threading import Timer
from time import sleep
from winsound import PlaySound, SND_ASYNC
class MyTimer():
def __init__(self, t, dialog):
self.timer = Timer(t, self.Run)
self.dialog = dialog
self.timer.start()
def Run(self):
try:
edit = self.dialog.GetSizer().GetChildren()[2].GetWindow()
edit.SetValue("Too late !!!")
edit.SetSelection(-1,-1)
PlaySound('SystemExclamation', SND_ASYNC)
sleep(3)
PlaySound('SystemExclamation', SND_ASYNC)
self.dialog.Show(False)
self.dialog.MakeModal(False)
self.dialog.Destroy()
except:
pass
def Cancel(self):
self.timer.cancel()
class myDialog(wx.Dialog):
def __init__(self):
wx.Dialog.__init__(
self,
None,
-1,
"Self-timeout-aborted dialog",
style=wx.CAPTION | wx. STAY_ON_TOP
)
label = wx.StaticText(self,-1,"Enter number between 1 and 100:")
edit = wx.TextCtrl(self,-1,"")
message = "Only positive numbers between 1 and 100 please!"
w = edit.GetTextExtent(message)
edit.SetMinSize((10+w[0],-1))
buttons = self.CreateSeparatedButtonSizer(wx.OK|wx.CANCEL)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(label,0,wx.TOP|wx.LEFT|wx.RIGHT, 15)
sizer.Add((1,3))
sizer.Add(edit,0,wx.EXPAND|wx.LEFT|wx.BOTTOM|wx.RIGHT, 15)
sizer.Add(buttons, 0, wx.EXPAND|wx.ALL, 5)
self.SetSizer(sizer)
self.SetAutoLayout(True)
sizer.Fit(self)
self.MakeModal(True)
self.Show(True)
edit.SetFocus()
timer=MyTimer(t = 10.0, dialog = self)
def onButton(evt):
timer.Cancel()
if evt.GetId() == wx.ID_OK:
try:
value = int(edit.GetValue())
if value >= 1 and value <= 100:
value = value * 255
value = value / 100
eg.plugins.BroadcastListener.Broadcast(u'{eg.event.string}', str(value), 3456)
self.Show(False)
self.MakeModal(False)
self.Destroy()
else:
edit.SetValue(message)
edit.SetFocus()
edit.SetSelection(-1,-1)
except ValueError:
edit.SetValue(message)
edit.SetFocus()
edit.SetSelection(-1,-1)
else:
self.Show(False)
self.MakeModal(False)
self.Destroy()
self.Bind(wx.EVT_BUTTON, onButton)
wx.CallAfter(myDialog)
You may need to adjust this row:
Code: Select all
eg.plugins.BroadcastListener.Broadcast(u'{eg.event.string}', str(value), 3456)
Pako