wx python检测用户是否未进行5分钟的操作然后执行某些操作

时间:2017-09-27 02:37:01

标签: python python-3.x wxpython

这是一个简单的代码,我正在使用WX python,我想如果用户没有在1分钟内指示它会做一些事情。

import wx    
class QuestAsk(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None,
                          pos=wx.DefaultPosition, size=wx.Size(400, 100),
                          style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION |
                                wx.CLOSE_BOX | wx.CLIP_CHILDREN,
                          title="Question")
        mainwindows = wx.Panel(self)
        mysizer = wx.BoxSizer(wx.VERTICAL)        
        self.txt = wx.TextCtrl(mainwindows, style=wx.TE_PROCESS_ENTER, size=(100, 30))
        self.txt.SetFocus()
        self.txt.Bind(wx.EVT_TEXT_ENTER, self.StatNow)
        mysizer.Add(self.txt, 15, wx.ALL, 5)
        mainwindows.SetSizer(mysizer)
        self.Show()
    def StatNow(self, event):
        go = self.txt.GetValue()
        go = go.lower()
        link = go.split()
        self.txt.Clear()
        self.txt.SetValue(go)
        if go.startswith(''):
            try:
               #request
            except Exception as e:
                print(str(e))


if __name__ == "__main__":
    app = wx.App(True)
    frame = QuestAsk()
    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

最简单的方法是添加wx.Timer,如下所示:

import wx
class QuestAsk(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None,
                          pos=wx.DefaultPosition, size=wx.Size(400, 100),
                          style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION |
                                wx.CLOSE_BOX | wx.CLIP_CHILDREN,
                          title="Question")
        mainwindows = wx.Panel(self)
        mysizer = wx.BoxSizer(wx.VERTICAL)
        self.txt = wx.TextCtrl(mainwindows, style=wx.TE_PROCESS_ENTER, size=(100, 30))
        self.txt.SetFocus()
        self.txt.Bind(wx.EVT_TEXT_ENTER, self.StatNow)

        # Add a timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(60000)

        mysizer.Add(self.txt, 15, wx.ALL, 5)
        mainwindows.SetSizer(mysizer)
        self.Show()

    def OnTimer(self, evt):
        print ("Timeout! Please input something")
        self.timer.Stop()
        self.timer.Start(60000)

    def StatNow(self, event):
        self.timer.Stop()
        go = self.txt.GetValue()
        go = go.lower()
        link = go.split()
        self.txt.Clear()
        self.txt.SetValue(go)
        if go.startswith(' '):
            try:
               print ("Go is empty")
            except Exception as e:
                print(str(e))
        self.timer.Start(60000)

if __name__ == "__main__":
    app = wx.App(True)
    frame = QuestAsk()
    app.MainLoop()

注意:计时器的工作时间为毫秒。我已将计时器设置为1分钟

编辑以满足按键重启计时器的要求 逻辑上,只有在用户提交了答案即按下回车后,才会发生做某事的指令。但是,如果您希望在密钥凹陷之间使用计时器,那么只需使用EVT_TEXT事件并将其绑定到重新启动计时器的函数。

import wx
class QuestAsk(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None,
                          pos=wx.DefaultPosition, size=wx.Size(400, 100),
                          style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION |
                                wx.CLOSE_BOX | wx.CLIP_CHILDREN,
                          title="Question")
        mainwindows = wx.Panel(self)
        mysizer = wx.BoxSizer(wx.VERTICAL)
        self.txt = wx.TextCtrl(mainwindows, style=wx.TE_PROCESS_ENTER, size=(100, 30))
        self.txt.SetFocus()
        self.txt.Bind(wx.EVT_TEXT_ENTER, self.StatNow)
        self.txt.Bind(wx.EVT_TEXT, self.CharNow)

        # Add a timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(10000)

        mysizer.Add(self.txt, 15, wx.ALL, 5)
        mainwindows.SetSizer(mysizer)
        self.Show()

    def OnTimer(self, evt):
        self.timer.Stop()
        wx.MessageBox('Please input something and press enter', 'Timeout', wx.OK | wx.ICON_INFORMATION)
        self.timer.Start(10000)

    def CharNow(self, event):
        self.timer.Stop()
        self.timer.Start(10000)

    def StatNow(self, event):
        self.timer.Stop()
        go = self.txt.GetValue()
        go = go.lower()
        link = go.split()
        self.txt.SetValue(go)
        if go.startswith(' '):
            wx.MessageBox('Input is Empty. Please input something and press enter', 'Timeout', wx.OK | wx.ICON_ERROR)
        self.timer.Start(10000)

if __name__ == "__main__":
    app = wx.App(True)
    frame = QuestAsk()
    app.MainLoop()

注意:我已将计时器设置为10秒进行测试。