gpt4 book ai didi

Python 和 PyQt : Catch Minimize Event

转载 作者:行者123 更新时间:2023-12-05 09:21:07 26 4
gpt4 key购买 nike

呃...有人可以帮我解决这个问题吗?我似乎无法捕获我创建的小部件的最小化事件。我尝试了我能找到的每篇文章中的解决方案,但它似乎不适用于我的代码。在我尝试过的一种解决方案中,窗口最小化并显示系统托盘图标,但应用程序图标在任务栏上仍然可见。我似乎无法弄清楚我应该做什么或我在这里缺少什么。我正在使用 Python 2.7 和 PyQt 4.11。我的操作系统是 Windows 7,所以我很确定这不是操作系统问题。如果有人能在这里帮助我,我将不胜感激。

这是代码。

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(385, 277)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.radioButton = QtGui.QRadioButton(Form)
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.gridLayout.addWidget(self.radioButton, 0, 1, 1, 1)
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.gridLayout.addWidget(self.lineEdit, 1, 0, 1, 1)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 1, 1, 1, 1)
self.listWidget = QtGui.QListWidget(Form)
self.listWidget.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked|QtGui.QAbstractItemView.EditKeyPressed|QtGui.QAbstractItemView.SelectedClicked)
self.listWidget.setSelectionBehavior(QtGui.QAbstractItemView.SelectItems)
self.listWidget.setMovement(QtGui.QListView.Snap)
self.listWidget.setUniformItemSizes(False)
self.listWidget.setObjectName(_fromUtf8("listView"))
self.gridLayout.addWidget(self.listWidget, 2, 0, 1, 2)
#system tray
self.systemTrayIcon = QtGui.QSystemTrayIcon()
self.systemTrayIcon.setIcon(QtGui.QIcon(":/images/images/mad-icon.jpg"))
self.systemTrayIcon.setVisible(True)
self.systemTrayIcon.activated.connect(self.sysTrayIconActivated)

self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.radioButton.setText(_translate("Form", "monitor clipboard", None))
self.pushButton.setText(_translate("Form", "Add", None))

#when system tray icon is clicked, if window is visible, then hide. if it is not visible, then show
def sysTrayIconActivated(self, reason):
if reason == QtGui.QSystemTrayIcon.Trigger:
if Form.isHidden():
Form.show()
else:
Form.hide()

if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())

最佳答案

这段代码应该可以工作

def changeEvent(self, event):
if event.type() == QEvent.WindowStateChange:
if event.oldState() and Qt.WindowMinimized:
print("WindowMinimized")
elif event.oldState() == Qt.WindowNoState or self.windowState() == Qt.WindowMaximized:
print("WindowMaximized")

关于Python 和 PyQt : Catch Minimize Event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34241202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com