此小应用程序在关闭或最小化后将隐藏在托盘中。我想弹出一个QColorDialog来选择一种颜色。这是代码:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDesktopWidget, QLineEdit, \
QSystemTrayIcon, QMenu, QAction, QTabWidget, QVBoxLayout, \
QPushButton, QTableWidget, QHeaderView, QMessageBox, \
QGridLayout, QPushButton, QTableWidgetItem, \
QAbstractItemView, QHBoxLayout, QLayout
from PyQt5.QtGui import QIcon, QPixmap, QDropEvent
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5 import QtCore
class App(QWidget):
def __init__(self):
super().__init__()
self.left = 10
self.top = 10
self.width = 460
self.height = 360
self.initUI()
# create tray icon
self.initTrayIcon()
# create tab1, parent is APP
self.initTab1()
self.initTab2()
self.initTab()
def initUI(self):
self.setFixedSize(self.width, self.height)
self.setWindowFlags(QtCore.Qt.Tool | QtCore.Qt.FramelessWindowHint)
self.setWindowOpacity(0.95)
ag = QDesktopWidget().availableGeometry()
self.fixedGeometry = (ag.width() - self.width - 20,
ag.height() - self.height - 5,
self.width,
self.height)
self.setGeometry(*self.fixedGeometry)
self.show()
def initTrayIcon(self):
self.tray_icon = SystemTrayIcon(self)
self.tray_icon.show()
def initTab(self):
# init tab
self.layout = QVBoxLayout()
self.tabs = QTabWidget()
self.tabs.resize(458, 358)
self.tabs.addTab(self.tab1, "T1")
self.tabs.addTab(self.tab2, 'T2')
self.bottom = QHBoxLayout()
self.pinButton = QtWidgets.QPushButton('Color')
self.pinButton.setMaximumSize(30, 30)
self.pinButton.clicked.connect(self.winPinTop)
self.bottom.addWidget(self.pinButton,alignment=QtCore.Qt.AlignRight)
self.bottom.setContentsMargins(1, 1, 1, 1)
# add tabs to widget
self.layout.addWidget(self.tabs)
self.layout.addLayout(self.bottom)
self.layout.setContentsMargins(0, 1, 0, 2)
self.setLayout(self.layout)
# tab1
def initTab1(self):
self.tab1 = QWidget()
self.tab1.layout = QVBoxLayout()
lb = QLabel()
lb.setText('tab1')
self.tab1.layout.addWidget(lb)
self.tab1.setLayout(self.tab1.layout)
# tab2
def initTab2(self):
self.tab2 = QWidget()
self.tab2.layout = QVBoxLayout()
lb = QLabel()
lb.setText('tab2')
self.tab2.layout.addWidget(lb)
self.tab2.setLayout(self.tab2.layout)
# hide to system tray instead of close
def closeEvent(self, event):
print('APP close')
event.ignore()
self.hide()
# display window in the right bottom
def rightBottomShow(self):
self.setGeometry(*self.fixedGeometry)
self.show()
def winPinTop(self):
color_dialog = QtWidgets.QColorDialog()
color_dialog.setOption(QtWidgets.QColorDialog.DontUseNativeDialog)
color = color_dialog.getColor()
if color.isValid():
print(color.name())
def showMenu(self):
print('menu')
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, parent=None):
super(SystemTrayIcon, self).__init__(parent)
icon = QIcon("icons/icon1.png")
QSystemTrayIcon.__init__(self, icon, parent)
menu = QMenu(parent)
showAction = menu.addAction('Show')
showAction.triggered.connect(self.parent().rightBottomShow)
menu.addSeparator()
exitAction = menu.addAction('Exit')
exitAction.triggered.connect(self.exit)
self.setContextMenu(menu)
self.activated.connect(self.onTrayIconActivated)
def exit(self):
QtCore.QCoreApplication.exit()
def onTrayIconActivated(self, reason):
#print(reason, '--reason--')
if reason == QSystemTrayIcon.Trigger:
#print(self.parent.frameGeometry(), self.parent.normalGeometry(), ag, sg)
if self.parent().isHidden():
self.parent().rightBottomShow()
else:
self.parent().hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
在QColorDialog中单击“取消”或“选择”按钮后,整个应用程序将关闭,没有任何错误。该代码在Ubuntu 19.04中运行。
我的代码中应该有一些错误。
我发现了为什么会这样,这全都与我设置的 WindowFlags 有关。删除QtCore.Qt.Tool
标志后,它可以正常工作。也许QColorDialog具有相同的窗口标志?但是我想从任务栏隐藏窗口。