简体   繁体   English

如何在单独的 .py 中向小部件 (PyQt5) 添加功能

[英]How to add functionality to widgets (PyQt5 ) in separate .py

I'm struggling to understand the general structure of how to work with PyQt.我正在努力理解如何使用 PyQt 的一般结构。 I want to have my main file我想要我的主文件

class MainWindow(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)


if __name__ == '__main__':
    app = qtw.QApplication([])
    widget = MainWindow()
    widget.show()
    app.exec_()

separate from the ui-file与 ui 文件分开

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")

    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.LeftSide = QtWidgets.QWidget(self.centralwidget)
    self.LeftSide.setObjectName("LeftSide")
    self.verticalLayout = QtWidgets.QVBoxLayout(self.LeftSide)
    self.excel.setObjectName("something")
    self.verticalLayout.addWidget(self.something)
    ...

as I want to be able to change the ui later on but retain all functionality.因为我希望以后能够更改用户界面但保留所有功能。 The UI has widgets within widgets. UI 在小部件中具有小部件。

How do I access these widgets in the separate main.py file or in separate files and add functionality, preferably by creating a class for each widget?如何在单独的 main.py 文件或单独的文件中访问这些小部件并添加功能,最好为每个小部件创建一个类?

Thanks a lot for any help!非常感谢您的帮助!

Basically you don't even have to convert .ui file to .py.基本上,您甚至不必将 .ui 文件转换为 .py。 You can load it to your window with:您可以使用以下命令将其加载到您的窗口:

from PyQt5 import uic
class MainWindow(qtw.QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        uic.loadUi("path/filename.ui",self)
        self.setUI()

Then you can access your widgets from ui like:然后您可以从 ui 访问您的小部件,例如:

self.centralWidget

For adding different functionalities to objects from ui file there are several options:要从 ui 文件向对象添加不同的功能,有几个选项:

  • Monkey patching (not really recommended but ok for small changes).猴子补丁(不是真的推荐,但可以进行小的更改)。
  • Promoting a widget using QtCreator or QtDesigner.使用 QtCreator 或 QtDesigner 提升小部件。 Basically you create a subclass of let's say QWidget in your MyWidget.py file, name that class MyWidget and add all needed functionalities.基本上,您在 MyWidget.py 文件中创建一个假设为 QWidget 的子类,将该类命名为 MyWidget 并添加所有需要的功能。 Then you place QWidget on your form in either QtCreator or QtDesigner click with RMB on it and click Promote to ... .然后你将 QWidget 放在你的表单上,在 QtCreator 或 QtDesigner 中单击并单击 RMB 并单击提升为 ...。 Then you specify path to your MyWidget.py file in field Header file and class name to Promoted class name .然后在字段Header file中指定 MyWidget.py 文件的路径,并将类名指定为Promoted class name This will replace standard widget with your custom when loading ui file.这将在加载 ui 文件时用您的自定义替换标准小部件。 Useful link: https://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17893.html .有用的链接: https ://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17893.html。

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

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