当 window 关闭时,从托盘图标弹出 window 不会关闭程序
popup window from tray icon that doesn't close the program when the window is closed
设置一个从托盘图标启动不同应用程序的工具包,我需要能够打开配置 window 然后关闭它而不关闭整个应用程序。
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()
我假设它关闭了整个应用程序,因为我的弹出窗口 window 是 Qmainwindow()
但我发现的唯一其他弹出类型 windows 是自动填充的对话框 windows与领域。也许我需要在托盘图标启动时启动 mainwindow,然后 hide() the mainwindow?然后启动弹出窗口 windows 并将其作为父项?
最终目标:我想从托盘图标中 select 选项并获得 windows 与我的配置信息一起出现。当有人在其中一个 windows 中单击 "okay"、"save"、"cancel" 等或单击 X 我不希望它退出应用程序并删除托盘图标。
如果您不希望在按 x 或关闭销售时关闭您的应用,则必须使用 setQuitOnLastWindowClosed(False)
。
def main(self):
app = QApplication(sys.argv)
QApplication.setQuitOnLastWindowClosed(False)
self.trayIcon = QSystemTrayIcon(QIcon("icons\icon-windowed.ico"), app)
设置一个从托盘图标启动不同应用程序的工具包,我需要能够打开配置 window 然后关闭它而不关闭整个应用程序。
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()
我假设它关闭了整个应用程序,因为我的弹出窗口 window 是 Qmainwindow()
但我发现的唯一其他弹出类型 windows 是自动填充的对话框 windows与领域。也许我需要在托盘图标启动时启动 mainwindow,然后 hide() the mainwindow?然后启动弹出窗口 windows 并将其作为父项?
最终目标:我想从托盘图标中 select 选项并获得 windows 与我的配置信息一起出现。当有人在其中一个 windows 中单击 "okay"、"save"、"cancel" 等或单击 X 我不希望它退出应用程序并删除托盘图标。
如果您不希望在按 x 或关闭销售时关闭您的应用,则必须使用 setQuitOnLastWindowClosed(False)
。
def main(self):
app = QApplication(sys.argv)
QApplication.setQuitOnLastWindowClosed(False)
self.trayIcon = QSystemTrayIcon(QIcon("icons\icon-windowed.ico"), app)