Python: How to distinguish between long, short button press and click - python

I want to have a simple GUI with 4 buttons. If you just click the button, function A should be executed, for short button press (e.g.1sec) function B should be executed and finally a long press (e.g. > 2s) function C should be executed.
Imagine a counter.
If you click the button, it will be reset to 0
If you short press the button, counter will be increased by 1 for e.g t=1sec
If you long press the button, counter will be increased by 10 until button is released.
Is somebody haveing an idea. I was trying this to accomplish it with a 2nd thread but I haven't found a possibility to stop the thread like you can start it

This is easy to do in PyQt if you use a widget which inherits QAbstractButton. No need for any timers or separate threads. Just use the built-in auto-repeat functionality, and keep a record of the current state.
Here's a simple demo:
from PyQt4 import QtGui, QtCore
class Button(QtGui.QPushButton):
def __init__(self, *args, **kwargs):
QtGui.QPushButton.__init__(self, *args, **kwargs)
self.setAutoRepeat(True)
self.setAutoRepeatDelay(1000)
self.setAutoRepeatInterval(1000)
self.clicked.connect(self.handleClicked)
self._state = 0
def handleClicked(self):
if self.isDown():
if self._state == 0:
self._state = 1
self.setAutoRepeatInterval(50)
print 'press'
else:
print 'repeat'
elif self._state == 1:
self._state = 0
self.setAutoRepeatInterval(1000)
print 'release'
else:
print 'click'
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
button = Button('Test Button')
button.show()
sys.exit(app.exec_())

in wxPython I would do it like this ... however you may need to adjust it for your GUI library ...
start_time = None
def onLeftDown(e):
global running
running = True
ct =0
while running:
ct += 1
do_something(ct)
def onLeftUp(e):
print "You Pressed For %s Seconds!"%(time.time()-start_time)
my_btn = wx.Button(parent,-1,"Click Me!")
my_btn.Bind(wx.EVT_LEFT_DOWN,onLeftDown)
my_btn.Bind(wx.EVT_LEFT_UP,onLeftUp)
Im not very familliar with QT but maybe you can modify this wx code to do what you want...
import wx
ct = 0
def counting():
global running
global ct
if running:
ct +=1
print ct
wx.CallLater(1,counting)
else:
print "OK DONE COUNTING AT:",ct
def onLeftDown(evt):
global running
running = True
counting()
def onLeftUp(evt):
print "STOP NOW!!"
global running
running = False
a = wx.App(redirect=False)
f = wx.Frame(None,-1,"asdasd")
b = wx.Button(f,-1,"Click Me")
b.Bind(wx.EVT_LEFT_DOWN,onLeftDown)
b.Bind(wx.EVT_LEFT_UP,onLeftUp)
f.Show()
a.MainLoop()

Related

Create a GIF for Maya with python threading

I want to add a gif element in my custom Maya UI using python. Since Maya doens't accept animated images as input I created a while loop and edit the input image. To do to that I used Timer and everything works fine, untill I rerun the script. The old thread is somehow still active (altough I kill it every time I run the script)
This is the code I have. I have a start and stop button that work exactly as they are supposed to, but if I rerun the script, the old thread is still active.
from threading import Timer
import time
import maya.cmds as cmds
global t
global N
global B
def initGif():
global t
global N
global B
terminateGif()
N = 0
t = Timer(1.0, startGif)
t.start()
def startGif():
global N
global t
while N < 1000:
if N < 1000:
print "Hello", t # This is where the actual images would iterate instead of "Hello"
time.sleep(5)
else:
terminateGif()
continue
def terminateGif():
global t
global N
N = 9999999
try:
t.cancel()
except:
t = "None"
return
def UI():
if cmds.window("win", exists = True):
cmds.deleteUI("win")
cmds.window("win", w = 500, h = 500)
cmds.columnLayout()
cmds.button("stop", c = lambda *args : terminateGif())
cmds.button("start", c = lambda *args : initGif())
cmds.showWindow("win")
UI()
initGif()
The fact that you're trying to get a gif working with threading and a timer is just asking for Maya to crash, or at least slow down the scene's performance.
Instead of bearing all of the overhead, I strongly recommend you just use PySide, which is built-in to Maya anyways. Here's a simple example without having to deal with the nightmare that is threading:
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class Win(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
self.setWindowTitle("Gif Example")
self.resize(500, 500)
self.movie = QtGui.QMovie("/PATH/TO/YOUR/GIF.gif") # Set your gif path here.
self.movie.setScaledSize(QtCore.QSize(150, 150)) # You can resize it too.
self.movie.start()
self.gif = QtWidgets.QLabel(parent=self) # Use QLabel to display the gif.
self.gif.setMovie(self.movie)
self.main_layout = QtWidgets.QVBoxLayout()
self.main_layout.addWidget(self.gif)
self.setLayout(self.main_layout)
win = Win()
win.show()

Tkinter text entry with pyHook hangs GUI window

I have a Tkinter GUI application that I need to enter text in. I cannot assume that the application will have focus, so I implemented pyHook, keylogger-style.
When the GUI window does not have focus, text entry works just fine and the StringVar updates correctly. When the GUI window does have focus and I try to enter text, the whole thing crashes.
i.e., if I click on the console window or anything else after launching the program, text entry works. If I try entering text immediately (the GUI starts with focus), or I refocus the window at any point and enter text, it crashes.
What's going on?
Below is a minimal complete verifiable example to demonstrate what I mean:
from Tkinter import *
import threading
import time
try:
import pythoncom, pyHook
except ImportError:
print 'The pythoncom or pyHook modules are not installed.'
# main gui box
class TestingGUI:
def __init__(self, root):
self.root = root
self.root.title('TestingGUI')
self.search = StringVar()
self.searchbox = Label(root, textvariable=self.search)
self.searchbox.grid()
def ButtonPress(self, scancode, ascii):
self.search.set(ascii)
root = Tk()
TestingGUI = TestingGUI(root)
def keypressed(event):
key = chr(event.Ascii)
threading.Thread(target=TestingGUI.ButtonPress, args=(event.ScanCode,key)).start()
return True
def startlogger():
obj = pyHook.HookManager()
obj.KeyDown = keypressed
obj.HookKeyboard()
pythoncom.PumpMessages()
# need this to run at the same time
logger = threading.Thread(target=startlogger)
# quits on main program exit
logger.daemon = True
logger.start()
# main gui loop
root.mainloop()
I modified the source code given in the question (and the other one) so that the pyHook
related callback function sends keyboard event related data to a
queue. The way the GUI object is notified about the event may look
needlessly complicated. Trying to call root.event_generate in
keypressed seemed to hang. Also the set method of
threading.Event seemed to cause trouble when called in
keypressed.
The context where keypressed is called, is probably behind the
trouble.
from Tkinter import *
import threading
import pythoncom, pyHook
from multiprocessing import Pipe
import Queue
import functools
class TestingGUI:
def __init__(self, root, queue, quitfun):
self.root = root
self.root.title('TestingGUI')
self.queue = queue
self.quitfun = quitfun
self.button = Button(root, text="Withdraw", command=self.hide)
self.button.grid()
self.search = StringVar()
self.searchbox = Label(root, textvariable=self.search)
self.searchbox.grid()
self.root.bind('<<pyHookKeyDown>>', self.on_pyhook)
self.root.protocol("WM_DELETE_WINDOW", self.on_quit)
self.hiding = False
def hide(self):
if not self.hiding:
print 'hiding'
self.root.withdraw()
# instead of time.sleep + self.root.deiconify()
self.root.after(2000, self.unhide)
self.hiding = True
def unhide(self):
self.root.deiconify()
self.hiding = False
def on_quit(self):
self.quitfun()
self.root.destroy()
def on_pyhook(self, event):
if not queue.empty():
scancode, ascii = queue.get()
print scancode, ascii
if scancode == 82:
self.hide()
self.search.set(ascii)
root = Tk()
pread, pwrite = Pipe(duplex=False)
queue = Queue.Queue()
def quitfun():
pwrite.send('quit')
TestingGUI = TestingGUI(root, queue, quitfun)
def hook_loop(root, pipe):
while 1:
msg = pipe.recv()
if type(msg) is str and msg == 'quit':
print 'exiting hook_loop'
break
root.event_generate('<<pyHookKeyDown>>', when='tail')
# functools.partial puts arguments in this order
def keypressed(pipe, queue, event):
queue.put((event.ScanCode, chr(event.Ascii)))
pipe.send(1)
return True
t = threading.Thread(target=hook_loop, args=(root, pread))
t.start()
hm = pyHook.HookManager()
hm.HookKeyboard()
hm.KeyDown = functools.partial(keypressed, pwrite, queue)
try:
root.mainloop()
except KeyboardInterrupt:
quit_event.set()

How to interrupt a thread/process by making a correct "Stop" button in Tkinter (Python)?

I want some basics on the problem of making some sort of "Stop" button that in my case terminates the series of beeps:
from tkinter import *
import winsound
from random import randint
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.widgets()
def widgets(self):
self.beep = Button(self, text = "Beep", command = play_beep)
self.beep.pack()
self.stop = Button(self, text = "Stop", command = stop_beep)
self.stop.pack()
go_on = True
def play_beep():
count = 10
while go_on == True and count != 0:
winsound.Beep(randint(100, 2500), 200)
count -= 1
def stop_beep():
go_on = False
root = Tk()
app = App(root)
root.mainloop()
When I press the "Beep" button it gets stuck as well as all the GUI until the beeps end. Could anyone tell me how to fix it?
I don't use TKinter, but I believe your button press is not creating a separate thread or process. The reason why your button gets stuck is because your play_beep loop is blocking your GUI execution loop. So we use threading. The thread executes at the same time as your GUI, so you can basically do two things at once (listen for GUI events and play beep noises).
import threading
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.is_playing = False
self.pack()
self.widgets()
def widgets(self):
self.beep = Button(self, text = "Beep", command = self.play_beep)
self.beep.pack()
self.stop = Button(self, text = "Stop", command = self.stop_beep)
self.stop.pack()
def play_beep(self):
self.is_running = True
self.beep_th = threading.Thread(target=self.run)
self.beep_th.start()
def run(self):
count = 10
while self.is_running == True and count != 0:
winsound.Beep(randint(100, 2500), 200)
count -= 1
def stop_beep(self):
try:
self.is_running = False
self.beep_th.join(0)
self.beep_th = None
except (AttributeError, RuntimeError): # beep thread could be None
pass
def closeEvent(self, event): # This is a pyside method look for a TKinter equivalent.
"""When you close the App clean up the thread and close the thread properly."""
self.stop_beep()
super().closeEvent(event)
First off, your question has nothing to do about threads or processes. Tkinter is single-threaded.
If you want to run some function periodically in a tkinter program, you must give the event loop a chance to process events. The typical solution is to do it like this:
def play_beep(count=10):
if go_on and count != 0:
winsound.Beep(randint(100, 2500), 200)
root.after(1000, play_beep, count=1)
This will cause the beep to play every second (1000ms) for ten iterations. In between each call, the event loop will have a chance to process other events.
Now, if the code you are running takes a long time, you're going to have to run that code in a separate thread or process. I know nothing about winsound.Beep so I don't know if that's necessary or not.
Second, to be able to interrupt it, you need to make go_on global, otherwise you're simply setting a local variable that never gets used.
def stop_beek():
global go_on
go_on = False

Print while mouse pressed

i am using PyMouse(Event) for detecting if mouse button is pressed:
from pymouse import PyMouseEvent
class DetectMouseClick(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
def click(self, x, y, button, press):
if button == 1:
if press:
print("click")
else:
self.stop()
O = DetectMouseClick()
O.run()
This works so far, but now i want to loop print("click") until mouse isnt pressed anymore ... i tried:
def click(self, x, y, button, press):
if button == 1:
if press:
do = 1
while do == 1:
print("down")
if not press:
do = 0
And also smth. like:
while press:
print("click")
Someone can help me? Thanks!
I think as Oli points out in his comment there isn't a constant stream of clicks when the mouse button is held down so you'll have to have the print in a loop. Having the while loop running on the same thread prevents the click event firing when the mouse is released so the only way I can think of to achieve what you are after is to print("click") from a separate thread.
I'm not a Python programmer but I've had a stab which works on my machine (Python 2.7 on Windows 8.1):
from pymouse import PyMouseEvent
from threading import Thread
class DetectMouseClick(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
def print_message(self):
while self.do == 1:
print("click")
def click(self, x, y, button, press):
if button == 1:
if press:
print("click")
self.do = 1
self.thread = Thread(target = self.print_message)
self.thread.start()
else:
self.do = 0
print("end")
else:
self.do = 0
self.stop()
O = DetectMouseClick()
O.run()

nonblocking timer in python

I am developing an app in python with pyGtk+Glade.
I want to execute a function every N seconds (just like in javascript the function setTimeout()). If the user perform a click, some action must be done, and the timer must be reset.
I was trying with threading.Timer, something like this:
def callback():
print "callback executed"
t = threading.Timer(10.0, callback)
t.start()
but it doesn't work for me because it blocks everything for the N secs and doesn't capture the user click.
Any other option?
Since you're using PyGTK, your program should probably be using the g_main_loop, in which case you can call glib.timeout_add (interval, callback) to add a callback that gets called (roughly) every X seconds.
Here's an example:
import glib
import gtk
def yo ():
print "yo"
return True
glib.timeout_add (1000, yo)
gtk.main ()
Unfortunately, to reset the timeout I couldn't come up with an elegant solution. But you can create your own main loop so you have control over when the timeout resets, sorta like this:
import glib
import gtk
import time
timeout = 1;
timer = time.time() + timeout
while (True):
gtk.main_iteration (False)
if (timer <= time.time()):
print "Time up!"
timer = time.time() + timeout
This creates a timer which calls MainWin.update() every second. When the button is pressed, the current timer is killed and a new timer is started.
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import time
class MainWin(object):
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.button = gtk.Button("Reset")
self.button.connect("clicked", self.onclick)
self.window.add(self.button)
self.button.show()
self.window.show()
self.reset_timer = False
self.count = 0
self.duration = 1000
self.timer = gobject.timeout_add(self.duration, self.update)
def reset(self):
print('Resetting timer')
gobject.source_remove(self.timer)
# start a new period call to update
self.count = 0
self.timer = gobject.timeout_add(self.duration, self.update)
def onclick(self, widget):
# Every action which resets the timer should call self.reset_timer().
self.reset()
def update(self):
print('{t:.1f}: count = {i}'.format(t=time.time() % 100, i=self.count))
self.count += 1
return True
def main(self):
gtk.main()
if __name__=='__main__':
MainWin().main()

Categories