Python:如何合并Python类?

时间:2018-06-27 13:21:08

标签: python

经过多次失败尝试后,我发现了用于在github上构建python桌面应用程序的fbs模块教程。它提供了一个示例脚本以及如何将该脚本与fbs模块集成。

如何将示例应用程序中的类合并到“我的代码”中?

示例应用代码:

from fbs_runtime.application_context import ApplicationContext, \
    cached_property
from PyQt5.QtWidgets import QApplication, QMainWindow

class AppContext(ApplicationContext):
    def run(self):
        self.main_window.show()
        return self.app.exec_()
    @cached_property
    def main_window(self):
        result = QMainWindow()
        result.setWindowTitle('Hello World!')
        result.resize(250, 150)
        return result

我的代码:

    from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import QMessageBox, QAction, QPlainTextEdit, QPushButton

class Ui_Form(QtWidgets.QMainWindow):

        def setupUi(self, Form):
            Form.setObjectName("Form")
            Form.resize(846, 794)

            self.central_widget = QtWidgets.QWidget()
            Form.setCentralWidget(self.central_widget)

            self.gridLayout_2 = QtWidgets.QGridLayout(self.central_widget)
            self.gridLayout_2.setObjectName("gridLayout_2")
       ......


    if __name__ == '__main__':
        import sys

        # ~ app = QtWidgets.QApplication(sys.argv)
        # ~ Form = QtWidgets.QWidget()
        # ~ ui = Ui_Form()
        # ~ ui.setupUi(Form)
        # ~ Form.show()
        # ~ sys.exit(app.exec_())
        app = QtWidgets.QApplication(sys.argv)
        win = Ui_Form()
        app.exec_()

FBS回购:https://github.com/mherrmann/fbs-tutorial

1 个答案:

答案 0 :(得分:0)

合并课程?您可以详细说明吗?

如果您想使用Sample App中的类,可以对其进行扩展

class AppContext(ApplicationContext):
    ...

class MyNewClass(AppContext):
    # All atributes from AppContext will be in your MyNewClass

*编辑** 从github阅读有关该项目的文档后,您只需要实现 AppContext类,因为在此示例中,他已经从lib ApplicationContext

的外部类继承而来。

从fbs_runtime.application_context导入ApplicationContext,\     cached_property 从PyQt5.QtWidgets导入QApplication,QMainWindow

class AppContext(ApplicationContext):
        ...

    def run(self):
        # Here you can change or add your own logic
        self.main_window.show()
        return self.app.exec_()

    @cached_property
    def main_window(self):
        # Here you can change or add your own logic
        result = QMainWindow() # Probabily you should manipulate this instance adding whatever you want following lib docs at github
        result.setWindowTitle('Hello World!')
        result.resize(250, 150)
        return result