PyQt - 将 QAction 连接到函数 [英] PyQt - Connect QAction to function

查看:123
本文介绍了PyQt - 将 QAction 连接到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TrayIcon,我添加了一个退出"QAction,现在,我想在单击 TrayIcon 菜单中的退出时执行某个功能.这是我的代码:

I am using a TrayIcon, I have added a "Exit" QAction, and now, I want to execute a certain function when clicking Exit in the TrayIcon menu. Here is the code I have :

class TrayIcon(QSystemTrayIcon):
    """
    Displays a system tray icon
    """

    def __init__(self, interface: Interface) -> None:
        """
        Constructor
        :param interface: Interface to show when the tray icon is clicked
        """
        super().__init__(QIcon(resource_filename("ezstorage.resources.img.tray_icon", "folder.png")))
        self.interface = interface
        self.setVisible(True)
        self.show()
        self.activated.connect(self.clicked)
        menu = QMenu()
        action = QAction("Exit")
        menu.addAction(action)
        self.setContextMenu(menu)

推荐答案

这是我根据您的代码将菜单中的图标连接到功能的方式:

This is how I'd connect icons in the menu to functions according to your code:

self.menu = QMenu()
self.action = QAction("Exit")
self.menu.addAction(self.action)
self.action.triggered.connect(self.my_function)

函数 self.my_function 然后做任何你想做的事.

The function self.my_function then does whatever you'd like to have.

这篇关于PyQt - 将 QAction 连接到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆