有办法在PyQt4最小化到托盘吗?我已经与QSystemTrayIcon类,但现在我想减少或“隐藏”应用程序窗口,并只显示托盘图标。
有人做过吗?任何方向,我们将不胜感激。
使用Python和PyQt4 2.5.4窗口XP专业版
有办法在PyQt4最小化到托盘吗?我已经与QSystemTrayIcon类,但现在我想减少或“隐藏”应用程序窗口,并只显示托盘图标。
有人做过吗?任何方向,我们将不胜感激。
使用Python和PyQt4 2.5.4窗口XP专业版
它很简单,一旦你记住有年代无法真正减少< a href = " https://web.archive.org/web/20070114154431/http / /blogs.msdn.com/oldnewthing/archive/2003/09/10/54831.aspx " rel = " nofollow noreferrer " >系统托盘< / >。
相反,你假装这样做:
代码帮助,这里我写的应用程序,除了closeEvent而不是减少事件。
注:
“closeEvent(事件)”是一个覆盖Qt事件,所以必须把在课堂上实现你想要隐藏的窗口。
“okayToClose()”是一个函数你可以考虑实施(或您可能想要一个布尔标志商店)因为有时你真的想退出应用程序,而不是最小化到系统托盘。
还有一个例子,如何显示()你的窗口。
def __init__(self):
traySignal = "activated(QSystemTrayIcon::ActivationReason)"
QtCore.QObject.connect(self.trayIcon, QtCore.SIGNAL(traySignal), self.__icon_activated)
def closeEvent(self, event):
if self.okayToClose():
#user asked for exit
self.trayIcon.hide()
event.accept()
else:
#"minimize"
self.hide()
self.trayIcon.show() #thanks @mojo
event.ignore()
def __icon_activated(self, reason):
if reason == QtGui.QSystemTrayIcon.DoubleClick:
self.show()
只是为了增加由克里斯的例子:
<强> < /强>是至关重要的,你使用Qt符号声明信号时,即
正确<强> < / >强:
self.connect(self.icon, SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), self.iconClicked)
而不是PyQt之一
<强> < /强>不对、不工作:
self.connect(self.icon, SIGNAL("activated(QSystemTrayIcon.ActivationReason)"), self.iconClicked)
注意<代码>::> < /代码信号字符串。我花了三个小时才算出。
这年代工作代码。由于<强> Matze < /强>为<强> < /强>至关重要,好奇心的信号我花了更多的时间。但做其他事情。所以ta # !:-)
def create_sys_tray(self):
self.sysTray = QtGui.QSystemTrayIcon(self)
self.sysTray.setIcon( QtGui.QIcon( ../images/corp/blip_32.png ) )
self.sysTray.setVisible(True)
self.connect(self.sysTray, QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), self.on_sys_tray_activated)
self.sysTrayMenu = QtGui.QMenu(self)
act = self.sysTrayMenu.addAction("FOO")
def on_sys_tray_activated(self, reason):
print "reason-=" , reason
这是一个编辑vzades响应,但它被拒绝的理由。它一样的代码也遵守最小化事件(和运行没有语法错误/失踪的图标)。
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
style = self.style()
# Set the window and tray icon to something
icon = style.standardIcon(QtGui.QStyle.SP_MediaSeekForward)
self.tray_icon = QtGui.QSystemTrayIcon()
self.tray_icon.setIcon(QtGui.QIcon(icon))
self.setWindowIcon(QtGui.QIcon(icon))
# Restore the window when the tray icon is double clicked.
self.tray_icon.activated.connect(self.restore_window)
def event(self, event):
if (event.type() == QtCore.QEvent.WindowStateChange and
self.isMinimized()):
# The window is already minimized at this point. AFAIK,
# there is no hook stop a minimize event. Instead,
# removing the Qt.Tool flag should remove the window
# from the taskbar.
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.Tool)
self.tray_icon.show()
return True
else:
return super(Example, self).event(event)
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(
self,
Message ,"Are you sure to quit?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
self.tray_icon.show()
self.hide()
event.ignore()
def restore_window(self, reason):
if reason == QtGui.QSystemTrayIcon.DoubleClick:
self.tray_icon.hide()
# self.showNormal will restore the window even if it was
# minimized.
self.showNormal()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == __main__ :
main()
这是正确的方法来处理PyQt5双击托盘图标。
def _create_tray(self):
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.activated.connect(self.__icon_activated)
def __icon_activated(self, reason):
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick):
pass
这是代码,它确实帮助我相信给我的代码
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout, QSystemTrayIcon
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.icon = QSystemTrayIcon()
r = self.icon.isSystemTrayAvailable()
print r
self.icon.setIcon(QtGui.QIcon( /home/vzades/Desktop/web.png ))
self.icon.show()
# self.icon.setVisible(True)
self.setGeometry(300, 300, 250, 150)
self.setWindowIcon(QtGui.QIcon( /home/vzades/Desktop/web.png ))
self.setWindowTitle( Message box )
self.show()
self.icon.activated.connect(self.activate)
self.show()
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, Message , "Are you sure to quit?", QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
self.icon.show()
self.hide()
event.ignore()
def activate(self, reason):
print reason
if reason == 2:
self.show()
def __icon_activated(self, reason):
if reason == QtGui.QSystemTrayIcon.DoubleClick:
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == __main__ :
main()