У меня есть приложение, написанное на PyQt5, с основным пользовательским интерфейсом, наследуемым от QWidget, и значком на панели задач (QSystemTrayIcon). Если я закрою основной интерфейс с помощью кнопки закрытия (x), мой значок в области уведомлений останется на панели задач. Но если я нажму на значок, он снова не откроет основной интерфейс (QWidget). Как я могу добиться, чтобы основной интерфейс снова открывался при нажатии на значок?
Мой код (main.py):
from PyQt5.QtWidgets import QApplication, qApp
from PyQt5.QtMultimedia import QSound
import notify2
from PyQt5.QtCore import QTimer
from ui import UI
class App(UI):
def __init__(self):
super().__init__()
# add functionality to the appmenu
self.quit_app.triggered.connect(qApp.quit)
self.start_timer_menu.triggered.connect(self.startTimer)
self.stop_timer_menu.triggered.connect(self.stopTimer)
self.about.triggered.connect(self.aboutDialog)
notify2.init('Break Timer')
self.start.clicked.connect(self.startTimer)
def startTimer(self):
if self.minutes.value() == 1:
notify2.Notification(
'Timer started', f'You will receive a notification every minute.'
).show()
else:
notify2.Notification(
'Timer started', f'You will receive a notification every {self.minutes.value()} minutes.'
).show()
self.timer = QTimer()
self.stop.clicked.connect(self.stopTimer)
self.stop.setEnabled(True)
self.stop_timer_menu.setEnabled(True)
self.start.setDisabled(True)
self.start_timer_menu.setDisabled(True)
self.timer.timeout.connect(self.sendNotification)
self.timer.start(60000 * self.minutes.value())
def stopTimer(self):
notify2.Notification(
'Timer ended', f'You will not receive any further notifications.'
).show()
self.timer.stop()
self.stop.setDisabled(True)
self.stop_timer_menu.setDisabled(True)
self.start.setEnabled(True)
self.start_timer_menu.setEnabled(True)
def sendNotification(self):
if self.play_sounds.isChecked():
QSound.play("sound/notification.wav")
self.notification_pause = notify2.Notification(
'Take a break', 'Move and see away from the screen!')
self.notification_pause.show()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = App()
window.show()
sys.exit(app.exec_())
ui.py:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
class UI(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.resize(480, 360)
def initUI(self):
# menubar entries
# file
self.start_timer_menu = QAction("Start", self)
self.start_timer_menu.setShortcut("Ctrl+S")
self.stop_timer_menu = QAction("Stop", self)
self.stop_timer_menu.setShortcut("Ctrl+Alt+S")
self.stop_timer_menu.setDisabled(True)
self.quit_app = QAction("Quit", self)
self.quit_app.setShortcut("Ctrl+Q")
# help
self.about = QAction("About", self)
# menubar
self.menubar = QMenuBar(self)
self.file_menu = self.menubar.addMenu("File")
self.file_menu.addAction(self.start_timer_menu)
self.file_menu.addAction(self.stop_timer_menu)
self.file_menu.addAction(self.quit_app)
self.help_menu = self.menubar.addMenu("Help")
self.help_menu.addAction(self.about)
# widgets on the window
self.start = QPushButton(self)
self.start.setText("Start")
self.stop = QPushButton(self)
self.stop.setText("Stop")
self.stop.setDisabled(True)
self.interval = QLabel("Interval: ", self)
self.minutes = QSpinBox(self)
self.minutes.setMaximumWidth(125)
self.minutes.setRange(1, 99)
self.minutes.setValue(30)
self.minutes.setSuffix(" minutes")
self.minutes.valueChanged.connect(self.changeMinuteText)
self.play_sounds = QCheckBox(self)
self.play_sounds.setText("Play sounds")
# layout
self.hbox_btn = QHBoxLayout()
self.hbox_btn.addStretch(1)
self.hbox_btn.addWidget(self.start)
self.hbox_btn.addWidget(self.stop)
self.hbox_minutes = QHBoxLayout()
self.hbox_minutes.addWidget(self.interval)
self.hbox_minutes.addWidget(self.minutes)
self.vbox = QVBoxLayout()
self.vbox.addLayout(self.hbox_minutes)
self.vbox.addWidget(self.play_sounds)
self.vbox.addStretch(1)
self.vbox.addLayout(self.hbox_btn)
self.setLayout(self.vbox)
# system tray icon
self.tray = QSystemTrayIcon()
self.tray.setIcon(QIcon("icon/icon.png"))
self.tray.setVisible(True)
self.tray_menu = QMenu()
self.tray_menu.addAction(self.start_timer_menu)
self.tray_menu.addAction(self.stop_timer_menu)
self.tray_menu.addAction(self.quit_app)
self.tray.setContextMenu(self.tray_menu)
def changeMinuteText(self):
if self.minutes.value() == 1:
self.minutes.setSuffix(" minute")
else:
self.minutes.setSuffix(" minutes")
def aboutDialog(self):
self.about_dialog = QMessageBox()
self.about_dialog.setIcon(QMessageBox.Information)
self.about_dialog.setText("Break Timer is a little application, that regularly notifies you to take a break.")
self.about_dialog.setInformativeText(
"For more details see https://github.com/Palexer/break-timer")
self.about_dialog.setWindowTitle("About")
self.about_dialog.setStandardButtons(QMessageBox.Ok)
self.about_dialog.exec_()