在托盘中显示和隐藏我的PyQt应用程序并不像它应该的那样。

问题描述 投票:1回答:1

所以我创建了一个系统托盘菜单,我想在这里有隐藏应用程序(到托盘),退出它和显示它(打开主窗口)的选项。

出于某种原因,我能够通过添加一个 "隐藏 "选项来隐藏应用程序。self.win.hide() 命令,但我无法让它在我的托盘菜单中工作。

所以这是我的主窗口。

enter image description here

这是托盘菜单 This is the tray menu:

enter image description here

所以目前我一共有三个问题 So I have a total of 3 issues currently. 其中两个是托盘菜单的问题。

  1. 当我点击 "隐藏 "时,什么都没有发生。
  2. 当我使用 "最小化 "按钮隐藏它时,我试图点击 "显示 "来再次显示主窗口,但我看到的是这个。

enter image description here

这是我的代码

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow, QMenu, QStyle, QSystemTrayIcon
from PyQt5 import QtCore
import sys
from win10toast import ToastNotifier
import itertools
from PyQt5.QtGui import QIcon

DURATION_INT = 10
toaster = ToastNotifier()
TIME_CYCLER = itertools.cycle([10, 5])  # 10 minutes, 10 seconds

def secs_to_minsec(secs: int):
    mins = secs // 60
    secs = secs % 60
    minsec = f'{mins:02}:{secs:02}'
    return minsec


class App(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.current_timer = 1
        self.time_left_int = DURATION_INT
        self.myTimer = QtCore.QTimer(self)

        # Init QSystemTrayIcon
        self.tray_icon = QSystemTrayIcon(self)
        self.tray_icon.setIcon(self.style().standardIcon(QStyle.SP_ComputerIcon))

        #Tray menu
        show_action = QAction("Show", self)
        quit_action = QAction("Exit", self)
        hide_action = QAction("Hide", self)
        show_action.triggered.connect(self.show)
        hide_action.triggered.connect(self.hide)
        quit_action.triggered.connect(app.quit)
        tray_menu = QMenu()
        tray_menu.addAction(show_action)
        tray_menu.addAction(hide_action)
        tray_menu.addAction(quit_action)
        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

        # App window
        self.app = QApplication(sys.argv)
        self.win = QMainWindow()
        self.win.setGeometry(200, 200, 200, 200)
        self.win.setWindowTitle("test")

        # Widgets
        self.titleLabel = QtWidgets.QLabel(self.win)
        self.titleLabel.setText("Welcome to my app")
        self.titleLabel.move(50,20)

        self.timerLabel = QtWidgets.QLabel(self.win)
        self.timerLabel.move(50,50)
        self.timerLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.timerLabel.setStyleSheet("font: 10pt Helvetica")

        self.startButton = QtWidgets.QPushButton(self.win)
        self.startButton.setText("Start")
        self.startButton.move(50,100)
        self.startButton.clicked.connect(self.startTimer)

        self.minimizeButton = QtWidgets.QPushButton(self.win)
        self.minimizeButton.setText("Minimize")
        self.minimizeButton.move(50,130)
        self.minimizeButton.clicked.connect(self.minimize)

        self.update_gui()

        # Show window
        self.win.show()
        sys.exit(app.exec_())

    def startTimer(self):
        self.time_left_int = next(TIME_CYCLER)
        self.myTimer.timeout.connect(self.timerTimeout)
        self.myTimer.start(1000)

    def timerTimeout(self):
        self.time_left_int -= 1
        if self.time_left_int == 0:
            if self.current_timer == 1:
                toaster.show_toast("test1", "test1", duration=3, threaded=True)
                self.current_timer = 2
            elif self.current_timer == 2:
                toaster.show_toast("test2", "test2", duration=3, threaded=True)
                self.current_timer = 1
            self.time_left_int = next(TIME_CYCLER)

        self.update_gui()

    def update_gui(self):
        minsec = secs_to_minsec(self.time_left_int)
        self.timerLabel.setText(minsec)

    def minimize(self):
        self.win.hide()

    def closeEvent(self, event):
        event.ignore()
        self.win.hide()
        self.tray_icon.showMessage(
            "Tray Program",
            "Application was minimized to Tray",
            QSystemTrayIcon.Information,
            2000
        )

app = QtWidgets.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
main_window = App()
main_window.show()
sys.exit(app.exec_())

第三个与托盘菜单无关的问题是 每当我点击 "退出 "按钮时 应用程序就会被最小化到托盘并被隐藏起来 但由于某种原因弹出的信息没有显示出来 这很奇怪,因为如果我会在它被最小化后点击 "显示",然后再点击 "X",它就会一直在每次点击时显示弹出信息。不知道我错过了什么。

python pyqt
1个回答
1
投票

我没有 win10toast 模块,所以我注释了一些行,并用 # win10toast - 删掉它们。

我不明白为什么你要创建一个名为 self.win 对象? 试试这样。

import sys
import itertools
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow, QMenu, QStyle, QSystemTrayIcon
from PyQt5 import QtCore
from PyQt5.QtGui import QIcon

#from win10toast import ToastNotifier                                      # win10toast

DURATION_INT = 10
#toaster = ToastNotifier()                                                 # win10toast
TIME_CYCLER = itertools.cycle([10, 5])  

def secs_to_minsec(secs: int):
    mins = secs // 60
    secs = secs % 60
    minsec = f'{mins:02}:{secs:02}'
    return minsec


class App(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.current_timer = 1
        self.time_left_int = DURATION_INT
        self.myTimer = QtCore.QTimer(self)

        # Init QSystemTrayIcon
        self.tray_icon = QSystemTrayIcon(self)
        self.tray_icon.setIcon(self.style().standardIcon(QStyle.SP_ComputerIcon))

        #Tray menu
        show_action = QAction("Show", self)
        quit_action = QAction("Exit", self)
        hide_action = QAction("Hide", self)
        show_action.triggered.connect(self.show)
        hide_action.triggered.connect(self.hide)
        quit_action.triggered.connect(app.quit)

        tray_menu = QMenu()
        tray_menu.addAction(show_action)
        tray_menu.addAction(hide_action)
        tray_menu.addAction(quit_action)
        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

        # App window
# ?       self.app = QApplication(sys.argv)
# ?       self.win = QMainWindow()
# ?       self.win.setGeometry(200, 200, 200, 200)
# ?       self.win.setWindowTitle("test")
        self.setGeometry(200, 200, 200, 200)                                           # +++
        self.setWindowTitle("test")                                                    # +++

        # Widgets
        self.titleLabel = QtWidgets.QLabel(self) #.win)
        self.titleLabel.setText("Welcome to my app")
        self.titleLabel.move(50,20)

        self.timerLabel = QtWidgets.QLabel(self) #.win)
        self.timerLabel.move(50,50)
        self.timerLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.timerLabel.setStyleSheet("font: 10pt Helvetica")

        self.startButton = QtWidgets.QPushButton(self) #.win)
        self.startButton.setText("Start")
        self.startButton.move(50,100)
        self.startButton.clicked.connect(self.startTimer)

        self.minimizeButton = QtWidgets.QPushButton(self) #.win)
        self.minimizeButton.setText("Minimize")
        self.minimizeButton.move(50,130)
        self.minimizeButton.clicked.connect(self.minimize)

        self.update_gui()

        # Show window
# ?       self.win.show()
# ?       sys.exit(app.exec_())

    def startTimer(self):
        self.time_left_int = next(TIME_CYCLER)
        self.myTimer.timeout.connect(self.timerTimeout)
        self.myTimer.start(1000)

    def timerTimeout(self):
        self.time_left_int -= 1
        if self.time_left_int == 0:
            if self.current_timer == 1:
#                toaster.show_toast("test1", "test1", duration=3, threaded=True)    # win10toast
                self.current_timer = 2
            elif self.current_timer == 2:
#                toaster.show_toast("test2", "test2", duration=3, threaded=True)    # win10toast
                self.current_timer = 1
            self.time_left_int = next(TIME_CYCLER)

        self.update_gui()

    def update_gui(self):
        minsec = secs_to_minsec(self.time_left_int)
        self.timerLabel.setText(minsec)

    def minimize(self):
# ?       self.win.hide()
        self.hide()                                                                  # +++


    def closeEvent(self, event):
        event.ignore()
# ?       self.win.hide()
        self.hide()                                                                  # +++
        self.tray_icon.showMessage(
            "Tray Program",
            "Application was minimized to Tray",
            QSystemTrayIcon.Information,
            2000
        )

app = QtWidgets.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
main_window = App()
main_window.show()
sys.exit(app.exec_())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.