[PyQt] QMainWindow freezes
rennnyyy at nem-pro.de
rennnyyy at nem-pro.de
Mon Apr 15 11:57:18 BST 2013
Hey guys,
I have a problem with my project. First of all I want to describe what I'm trying to create and after that I explain the problems I have.
The project hooks all keyboard events that are done with the help of "pyHook" (I know that sounds like hacking now, but the software will only be used by myself). If specific keys are pressed they will be added to a dictionary which contains information about the time, when the key was pressed. Furthermore I wrote a GUI with the help of pyQt, which should display the difference between the current time and the time, when the key was pressed (more or less a timer that). To keep the displayed time difference up-to-date I have a infinite QTimer which call a function that recalculates the difference and display it in a QLabel. Here's my sourcecode:
class KeyboardHook(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def OnKeyboardEvent(self, event):
key = event.Key
if key in ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10'):
gui.OnAddKey(key)
return True
def run(self):
hookManager = pyHook.HookManager()
hookManager.KeyDown = self.OnKeyboardEvent
hookManager.HookKeyboard()
pythoncom.PumpMessages()
class GUI(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(460,276)
self.setWindowTitle('LoL Timer')
# Add KeyboardHook
self.keyboardHook = KeyboardHook()
# Timers
self.timers = {}
# Timer
self.timer = QtCore.QTimer();
QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.OnUpdate)
self.timer.start(100)
# Textsfields
self.textfields = {}
for i in xrange(1, 11):
tmp = QtGui.QLabel(QtCore.QString('-'), self)
if i % 2 == 1:
tmp.move(30, 30 + ((i - 1)/ 2) * 40)
else:
tmp.move(230, 30 + ((i - 1)/ 2) * 40)
self.textfields['F%s' %(i)] = tmp
def OnAddKey(self, key):
if key in self.timers:
del self.timers[key]
else:
self.timers[key] = time.time()
def OnUpdate(self):
for i in ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10'):
try:
self.textfields[i].setText(QtCore.QString(str(self.timers[i])))
except:
self.textfields[i].setText(QtCore.QString('-'))
The problem I've got: Sometimes I press one of the specific keys the GUI freezes. First I thought about a problem with the different threads, trying to access the same variable at one time. But when I changed the OnUpdate function to
def OnUpdate(self):
for i in ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10'):
test = self.timers[i]
try:
pass
except:
pass
the GUI never freezed (of course it did not update the timers). The variables that are accessed are the same, so I think the threads are not the problem.
Do you have any idea, what I'm doing wrong? Or is my whole concept of writing this software wrong?
Thanks in advance.
Rennnyyy
PS: The software is designed for helping me in a game, where I need accurate timers. That's why I need a global keyboard hook, to get key events even if my software has not the main focus.
More information about the PyQt
mailing list