Найти - Пользователи
Полная версия: problem with PyQT and SystemTray
Начало » GUI » problem with PyQT and SystemTray
1
tolen.maksut
Hello Everybody!!!
I have a problem with SystemTray and PyQt…
I want to write little project, which calculate how many times we press “ENTER”

when I minimize my program to “tray” and open another window then press “ENTER”, in this case program doesn't calucalate. How can I running my program in SystemTray?

my code is the following…

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui

class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, parent=None):
QtGui.QSystemTrayIcon.__init__(self, parent)
self.setIcon(QtGui.QIcon(“icon.jpg”))
self.mainMenu = QtGui.QMenu(parent)
#==================== M E N U ====================
systemInformation = self.mainMenu.addAction(“System Information”)
aboutButton = self.mainMenu.addAction(“About”)
exitButton = self.mainMenu.addAction(“Exit”)
self.setContextMenu(self.mainMenu)
#==================== EVENT ======================
self.connect(systemInformation,QtCore.SIGNAL('triggered()'),self.keyPressEvent)
self.connect(aboutButton,QtCore.SIGNAL('triggered()'),self.showAbout)
self.connect(exitButton,QtCore.SIGNAL('triggered()'),self.appExit)

self.show()

def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Enter: # IT DOESN'T WORK WHEN I MINIMIZE PROGRAM
print “some text”
def showAbout(self):
QtGui.QMessageBox.information(QtGui.QWidget(), self.tr(“About Tunarium”), self.tr(“Your text here.”))

def appExit(self):
sys.exit()

if __name__ == “__main__”:
app = QtGui.QApplication(sys.argv)

trayIcon = SystemTrayIcon()
trayIcon.show()
sys.exit(app.exec_())

Thanks!!!!
reclosedev
You can't capture key events when QT widget is not in focus. There is only platform depended ways to capture all key presses. Take a look at http://www.qtforum.org/article/33010/capturing-a-system-key-press-when-qt-form-is-not-in-focus-using-winevent.html

Also, if you only need to check if key is pressed on Windows, you can use GetAsyncKeyState function.

import win32api
import win32con
 
if win32api.GetKeyState(win32con.VK_RETURN) & 0x8000:
    print "some text"
tolen.maksut
reclosedev
You can't capture key events when QT widget is not in focus. There is only platform depended ways to capture all key presses. Take a look at http://www.qtforum.org/article/33010/capturing-a-system-key-press-when-qt-form-is-not-in-focus-using-winevent.htmlAlso, if you only need to check if key is pressed on Windows, you can use GetAsyncKeyState function.

I download win32api, then put this piece of code in my program
if win32api.GetKeyState(win32con.VK_LSHIFT) & 0x8000:
print “some text”

no error, I minimize my program to Tray, then I press “Left Shift” it doesn't work.
can you say me…
how can I connect my program with key Event listener?
reclosedev
You need to check GetAsyncKeyState() in loop or with timer.
Try this
http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial#tocpyHook_Tutorial4
tolen.maksut
Thank you very much!!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB