如何将函数绑定到 Qt 菜单栏中的操作?
- 作者: 我和老王不太熟
- 来源: 51数据库
- 2022-12-19
问题描述
我正在使用 Python3 和 PyQt5,在 Qt Designer 中制作我的小部件和窗口.更重要的是,我不会从 .ui 生成 .py 文件.我只是使用下一个代码加载它:
I'm using Python3 and PyQt5, make my widgets and windows in Qt Designer. What is more, I do not generate .py files from a .ui. I simply load it using next code:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('UI/Qt/source/MainWindow.ui', self)
所以,我想知道,如何将菜单栏操作绑定到函数.
So, I wanted to know, how do I bind menu bar actions to functions.
有什么办法可以做这样的事情吗?
Is there any way I can do something like this?
self.getActionByName("actionTest_Action").connect(self.do_something)
推荐答案
在使用loadUi的时候不需要使用findChild,因为该方法以objectName为名称将对象添加到类的属性中,例如在这个比另一个答案更简洁的代码是:
It is not necessary to use findChild when using loadUi since this method adds the object to the attributes of the class using the objectName as a name, for example in this particular case a cleaner code than the other answer is:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('UI/Qt/source/MainWindow.ui', self)
self.actionTest_Action.triggered.connect(self.test)
def test(self):
print("Test")
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
