0

I have two python files, one is the gui, the other contains all of the functions, say i have a class named Backend like in the code below, and one of its functions is "print1(self)", and i want to use that "print1" function inside of the second file in the class named retranslateUi(self, Form), how would i do it?

First file

__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')

Second file

# -*- 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"))

2 Answers 2

1

First you should create a main entry point to the application like so. I will call it app.py and the generated Qt Designer file view.py for this example:

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 follows the Model-View architecture so this is a nice way to bind your functionality / data away from the graphical components of the application.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to import the first file into the second one as import first_filename as t then create class object p=t.classname then use any function you want which is declared in the class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.