問題描述
我正在使用 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")
這篇關于如何將函數綁定到 Qt 菜單欄中的操作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!