设置一个从托盘图标启动不同应用程序的工具包,我需要能够打开配置窗口,然后在不关闭整个应用程序的情况下关闭它。
import sys
from PyQt5.QtWidgets import QSystemTrayIcon, QApplication, QMenu, qApp, QMainWindow, QPushButton, QLabel, QLineEdit
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore
class autoparse():
def __init__(self):
self.main()
def main(self):
app = QApplication(sys.argv)
self.trayIcon = QSystemTrayIcon(QIcon("icons\icon-windowed.ico"), app)
self.menu = QMenu()
self.trayIcon.setContextMenu(self.menu)
self.autopconfig = self.menu.addAction('Config')
self.autopconfig.triggered.connect(self.configwindow)
exitaction = self.menu.addAction("Exit")
exitaction.triggered.connect(qApp.quit)
self.trayIcon.show()
sys.exit(app.exec_())
def configwindow(self):
try:
self.config = QMainWindow()
self.config.setWindowTitle('Configuration')
self.config.setGeometry(300, 300, 640, 480)
self.lbl = QLabel('Directory: ', self.config)
self.lbl.setGeometry(QtCore.QRect(10, 20, 200, 20))
self.pathsel = QLineEdit(self.config)
self.pathsel.setMaxLength(250)
self.pathsel.setText('path here')
# self.pathsel.selectAll()
self.pathsel.setGeometry(QtCore.QRect(10, 50, 400, 20))
print(self.pathsel.text())
self.btn = QPushButton('...', self.config)
self.btn.setGeometry(QtCore.QRect(414, 50, 30, 20))
self.btn.clicked.connect(self.fileselect)
self.config.show()
except Exception as E:
print(E)
def fileselect(self):
print('hello')
test1 = autoparse()
我假设它关闭了整个应用程序,因为我的弹出窗口是Qmainwindow()
,但我发现的唯一其他弹出类型窗口是自动填充字段的对话框窗口。也许我需要在托盘图标启动时启动主窗口,然后隐藏()主窗口?然后以它作为父级启动弹出窗口?
最终目标:我想从托盘图标中选择选项并获取包含我配置信息的窗口。当有人在这些窗口之一中单击“确定”、“保存”、“取消”等或单击 XI 时,不希望它退出应用程序并删除托盘图标。