Уведомления

Группа в Telegram: @pythonsu

#1 Окт. 31, 2012 22:52:54

tolen.maksut
Зарегистрирован: 2012-10-31
Сообщения: 11
Репутация: +  0  -
Профиль  

problem with PyQT and SystemTray

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!!!!

Офлайн

#2 Ноя. 1, 2012 06:17:01

reclosedev
От: Н.Новгород
Зарегистрирован: 2012-03-29
Сообщения: 870
Репутация: +  173  -
Профиль   Отправить e-mail  

problem with PyQT and SystemTray

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"

Офлайн

#3 Ноя. 1, 2012 08:38:38

tolen.maksut
Зарегистрирован: 2012-10-31
Сообщения: 11
Репутация: +  0  -
Профиль  

problem with PyQT and SystemTray

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?

Офлайн

#4 Ноя. 1, 2012 10:25:13

reclosedev
От: Н.Новгород
Зарегистрирован: 2012-03-29
Сообщения: 870
Репутация: +  173  -
Профиль   Отправить e-mail  

problem with PyQT and SystemTray

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

Офлайн

#5 Ноя. 1, 2012 14:05:47

tolen.maksut
Зарегистрирован: 2012-10-31
Сообщения: 11
Репутация: +  0  -
Профиль  

problem with PyQT and SystemTray

Thank you very much!!

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version