使用另一个文件中 class 的函数到我的第二个文件 python3 中的 class

Using functions from a class in another file to a class in my second file, python3

我有两个 python 文件,一个是 gui,另一个包含所有功能,比如我有一个 class 名为 Backend 的代码,如下面的代码所示,它的一个功能是 "print1(self)",我想在名为 retranslateUi(self, Form) 的 class 中的第二个文件中使用 "print1" 函数,我该怎么做?

第一个文件

__author__ = 'Richard'
import sqlite3
conn = sqlite3.connect('realscheduler.db')
c = conn.cursor()
c.execute('pragma foreign_keys = ON;')
class Backend:
    def print1(self):
        print('Works')

第二个文件

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.label = QtWidgets.QLabel(Form)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem1)
        self.horizontalLayout.addLayout(self.verticalLayout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Labellabellabel"))
        self.pushButton.setText(_translate("Form", "Print"))

您必须将第一个文件作为 import first_filename as t 导入到第二个文件中,然后创建 class 对象 p=t.classname 然后使用您想要的在 [=14= 中声明的任何函数]

首先,您应该像这样创建应用程序的主入口点。对于这个例子,我将其命名为 app.py 和生成的 Qt Designer 文件 view.py

from PyQt5.QtWidgets import (QMainWindow, QApplication)

# importing the GUI from the designer file
from view import Ui_MainWindow

# importing the database file Backend class
from database import Backend

class Main(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)

        # this will connect your push button the the function
        self.pushButton.clicked.connect(Backend.print1)

if __name__ == "__main__":
    import sys

    QCoreApplication.setApplicationName("app name")
    QCoreApplication.setApplicationVersion("app version")
    QCoreApplication.setOrganizationName("Your name")
    QCoreApplication.setOrganizationDomain("Your URL")

    app = QApplication(sys.argv)
    form = Main()
    form.show()
    sys.exit(app.exec_())

PyQt 遵循模型-视图架构,因此这是将功能/数据与应用程序的图形组件绑定的好方法。