Python Forum

Full Version: set up the timer when I don't press on keyboard buttons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi yall,
I'm having difficulties with the python timer and I would greatly appreciate for some advice. 

I am working on keyboard control to disable the MyPlayer.Control_EPG_ID object in 5 seconds when I do not press on any buttons of the keyboard, but I have a bit of trouble with set up the timer. When I pressed on the down arrow buttons of the keyboard, it will set up the timer to 5 seconds but when I try to press on the right arrow button of the keyboard to change the text, it will not let me to change the text until to 5 seconds later.

When I try this:

    self.channel_pressed = False
    
    def onAction(self, action):
    
        if action.getId() in (ACTION_MOVE_UP, ACTION_MOVE_DOWN):
            self.channel_pressed = True
    
            if action.getId() == ACTION_MOVE_DOWN:
               if self.channel_pressed == True:
                  if self.EPG_MINI_GUIDE == False:
                      self.getControl(MyPlayer.Control_EPG_ID).setVisible(True)
                      
               time.sleep(5000)
               self.getControl(MyPlayer.Control_EPG_ID).setVisible(False)
    
    
    
        elif action.getId() in (ACTION_MOVE_LEFT, ACTION_MOVE_RIGHT):
             next_program = self.getControl(MyPlayer.Control_EPG_Program_Next).getLabel()
    
             if action == ACTION_MOVE_RIGHT:
                if self.EPG_MINI_GUIDE == True:
                   self.getControl(MyPlayer.Control_EPG_Program_Now).setLabel(next_program)
When I removed the time.sleep(5000, I can be able to change the text without have any problem when I press on the right arrow button of the keyboard, but the problem is the MyPlayer.Control_EPG_ID object will be disable in 1 second when I removed the time.sleep(5000). I want to use the time.sleep to set up the timer to 5 seconds when I don't press on any buttons of the keyboard so I can start to disable the control for the MyPlayer.Control_EPG_ID object in 5 seconds later. If I press any buttons of the keyboard, the timer should stop unless I don't press on any buttons of the keyboard.

I want to know how I could do that in python? can you please show me an example how I could do that using with my current code?

Thanks in advance
time.sleep simply stops the execution and that is why you are not able to edit. what you need is to delay the execution of the code that disable the control. I don't know what GUI you use, but for example Tkinter root and other Toplevel windows have method called after() and in this case you will put line14 in separate function and call it with delay. You should have something similar in your GUI framework too
in wxpython phoenix, it's wx.Timer (https://wxpython.org/Phoenix/docs/html/wx.Timer.html)
(Oct-05-2017, 07:50 AM)buran Wrote: [ -> ]time.sleep simply stops the execution and that is why you are not able to edit. what you need is to delay the execution of the code that disable the control. I don't know what GUI you use, but for example Tkinter root and other Toplevel windows have method called after() and in this case you will put line14 in separate function and call it with delay. You should have something similar in your GUI framework too

Yes that is correct, I want to delay the execution. Do you know what I need to use? time.start(5.0) or what?

It would be good if you can post the code so I can test it on my script.