简体   繁体   English

如何导入菜单按钮并在触发时运行 function? (PyQt5)

[英]How to import a menu button and run a function if triggered? (PyQt5)

I have imported a menu bar button from "Design.ui" and tried to assign functions to it but it seems like there is an error.我从“Design.ui”导入了一个菜单栏按钮,并尝试为其分配功能,但似乎出现错误。

Imported design.ui --> uic.loadUi("Design.ui", self)导入 design.ui --> uic.loadUi("Design.ui", self)

Then importing the menu button from design.ui self.openFileButton = self.findChild(QStatusBar, "actionOpen")然后从 design.ui 导入菜单按钮self.openFileButton = self.findChild(QStatusBar, "actionOpen")

(Error: 'NoneType' object has no attribute 'triggered' ) Then trying to assign functions in it and getting the error, self.openFileButton.triggered.connect(self.openImage) (Error: 'NoneType' object has no attribute 'triggered' ) 然后尝试在其中分配函数并得到错误, self.openFileButton.triggered.connect(self.openImage)

Thanks for helping:)感谢您的帮助:)

After calling uic.loadUi() , you should be able to to directly use the QWidgets you created in the .ui file instead of calling self.findChild()调用uic.loadUi()后,您应该能够直接使用您在.ui文件中创建的 QWidgets 而不是调用self.findChild()

For example:例如:

class MainWidget(QWidget):

   def __init__(self):
      QWidget.__init__(self)   
      uic.loadUi("Design.ui", self)
      self.openFileButton.triggered.connect(self.openImage)

   def openImage(self):
      # Does some code

Alternatively, you can set the loaded UI as a variable and use the widgets from there.或者,您可以将加载的 UI 设置为变量并使用那里的小部件。

For Example:例如:

class MainWidget(QWidget):

   def __init__(self):
      QWidget.__init__(self)   
      self.ui = uic.loadUi("Design.ui", self)
      self.ui.openFileButton.triggered.connect(self.openImage)

   def openImage(self):
      # Does some code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM