<div dir="ltr"><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">make sure u have something like this when u call QMainwindow<br><br>def main(argv):<br><br> app = QtGui.QApplication(argv)<br> yourMainWin = MainWindUI()<br>
yourMainWin.show()<br> app.exec_()<br> <br>if __name__ == "__main__":<br> sys.exit(main(sys.argv))<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 15, 2013 at 4:27 PM, <span dir="ltr"><<a href="mailto:rennnyyy@nem-pro.de" target="_blank">rennnyyy@nem-pro.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hey guys,<br>
<br>
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.<br>
<br>
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:<br>
<br>
class KeyboardHook(threading.Thread):<br>
def __init__(self):<br>
threading.Thread.__init__(self)<br>
self.start()<br>
<br>
def OnKeyboardEvent(self, event):<br>
key = event.Key<br>
if key in ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10'):<br>
gui.OnAddKey(key)<br>
return True<br>
<br>
def run(self):<br>
hookManager = pyHook.HookManager()<br>
hookManager.KeyDown = self.OnKeyboardEvent<br>
hookManager.HookKeyboard()<br>
pythoncom.PumpMessages()<br>
<br>
class GUI(QtGui.QMainWindow):<br>
def __init__(self):<br>
QtGui.QMainWindow.__init__(self)<br>
self.resize(460,276)<br>
self.setWindowTitle('LoL Timer')<br>
<br>
# Add KeyboardHook<br>
self.keyboardHook = KeyboardHook()<br>
<br>
# Timers<br>
self.timers = {}<br>
<br>
# Timer<br>
self.timer = QtCore.QTimer();<br>
QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.OnUpdate)<br>
self.timer.start(100)<br>
<br>
# Textsfields<br>
self.textfields = {}<br>
for i in xrange(1, 11):<br>
tmp = QtGui.QLabel(QtCore.QString('-'), self)<br>
<br>
if i % 2 == 1:<br>
tmp.move(30, 30 + ((i - 1)/ 2) * 40)<br>
else:<br>
tmp.move(230, 30 + ((i - 1)/ 2) * 40)<br>
self.textfields['F%s' %(i)] = tmp<br>
<br>
def OnAddKey(self, key):<br>
if key in self.timers:<br>
del self.timers[key]<br>
else:<br>
self.timers[key] = time.time()<br>
<br>
def OnUpdate(self):<br>
for i in ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10'):<br>
try:<br>
self.textfields[i].setText(QtCore.QString(str(self.timers[i])))<br>
except:<br>
self.textfields[i].setText(QtCore.QString('-'))<br>
<br>
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<br>
<br>
def OnUpdate(self):<br>
for i in ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10'):<br>
test = self.timers[i]<br>
try:<br>
pass<br>
except:<br>
pass<br>
<br>
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.<br>
<br>
Do you have any idea, what I'm doing wrong? Or is my whole concept of writing this software wrong?<br>
<br>
Thanks in advance.<br>
<br>
<br>
Rennnyyy<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><b></b>
<p><a href="http://feeds.feedburner.com/SanLuthraBlog" target="_blank"><img src="http://feeds.feedburner.com/SanLuthraBlog.gif?w=1&c=1&bb=zCxf" alt="San's personal blog"></a></p>
<form action="http://fb/a/headlineanimator/install" method="get" accept-charset="utf-8" target="_blank" onsubmit="return window.confirm("You are submitting information to an external page.\nAre you sure?");">
<p style="background-image:url('/fb/lib/images/icons/chameleon_red/add.gif')">
<b><br></b></p></form>
</div>