0

I all, I'm trying to create a little GUI of two buttons. I'm trying with the first implemented button and I want to execute another script with it. How can I do so? Here is the code from the .ui file that I converted in Python with

 python3 -m PyQt5.uic.pyuic -x test_GUI.ui -o test_GUI.py

This is the code of the GUI:

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(80, 70, 160, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Thank you for help

TforV
  • 135
  • 7

1 Answers1

1

Implementation of a UI created for PyQt is done by importing the generated file into your main script(s), as explained in the official guidelines about using Designer.

Please note, as this is very important: the files generated by pyuic should never be modified. NEVER.
Trying to do so without exactly knowing what you're doing (and why) is considered bad practice as it usually leads to unexpected behavior, confusion about the object structures and bugs; also, the moment you need to change anything in the UI you'll need to merge the new generated file with your existing code, which is something usually very difficult and prone to bugs.

Simply put, you import the generated file, and create a class that inherits from both the base Qt class and the ui class.

Then, to provide interaction, Qt uses signals and slots: Qt objects usually have some signals that are emitted whenever something happens, and you can connect those signals to functions that will react accordingly.
The syntax is simple, take this pseudo-code:

someQtObject.someSignal.connect(someFunction)

The above means: when someQtObject emits the signal someSignal, then call someFunction.

For buttons, the commonly used signal is clicked.

from PyQt5 import QtWidgets
from test_GUI import Ui_MainWindow
from myModule import myFunction

class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.myFunction)

    def myFunction(self):
        print('Hello world!')
        print(myFunction())


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
musicamante
  • 41,230
  • 6
  • 33
  • 58