I need help in linking a pushButton on my PyQt5 code so that it runs another .py file automatically.
I have tried using 'self.pushButton.clicked.connect' but this does not seem to work on PyQt5 code. I have attached the code for both my .py files below:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(519, 354)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(210, 140, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
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", "Open"))
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_())
The above code should produce a window with a single button. When that button is clicked it should execute/run the code below
algorithm.py
arrNumbers = []
i = 0
j = 0
n = 0
a = 0
sum = 0
temp = 0
print("How many numbers would you like to sort?:", end=' ')
n = eval(input())
for i in range(0, n):
print("Enter Number", i + 1, ":", end=' ')
a = eval(input())
arrNumbers.append(a)
for i in range(1, n):
for j in range(0, n - i):
if (arrNumbers[j] > arrNumbers[j + 1]):
temp = arrNumbers[j]
arrNumbers[j] = arrNumbers[j + 1]
arrNumbers[j + 1] = temp
print()
print("After pass:",i)
for k in range(0, n):
print(arrNumbers[k], end=' ')
print("[",i + 1, "biggest number(s) is(are) pushed to the end of the list ]")
print()
print("It was completed in",i,"passes")
print("The sorted list using Bubble Sort is: ", end=' ')
for i in range(0, n):
print(arrNumbers[i], end=' ')
Please can you help me with this issue. I have been searching for solutions but nothing has seemed to work.
Thanks
Command links are similar to radio buttons in that they are used to select from a set of mutually exclusive, related choices. Like radio buttons, command links are always presented in sets, never individually.
We set the window size using the setGeometry(left,top,width,height) method. The window title is set using setWindowTitle(title). Finally show() is called to display the window.
The simplest way to close a window is to click the right (Windows) or left (macOS) 'X' button on the title bar.
main.py
step 1: you need to import your algorithm.py. to do that this is the syntax: import filename
you can neglect the .py
step 2: connect the push button to the function that will call the processes in algorithm.py. this is the syntax: self.pushButton.clicked.connect(self.FuncName)
step 3: call the function in algorithm.py that will do the work. syntax: filename.FuncInFilename()
from PyQt5 import QtCore, QtGui, QtWidgets
import algorithm
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(519, 354)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(210, 140, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
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", "Open"))
self.pushButton.clicked.connect(self.OpenClick)
def OpenClick(self):
algorithm.FunctionAlgo()
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_())
algorithm.py (i added the function FunctionAlgo()
for this file because it is more proper and it will be easier)
def FunctionAlgo():
arrNumbers = []
i = 0
j = 0
n = 0
a = 0
sum = 0
temp = 0
print("How many numbers would you like to sort?:", end=' ')
n = eval(input())
for i in range(0, n):
print("Enter Number", i + 1, ":", end=' ')
a = eval(input())
arrNumbers.append(a)
for i in range(1, n):
for j in range(0, n - i):
if (arrNumbers[j] > arrNumbers[j + 1]):
temp = arrNumbers[j]
arrNumbers[j] = arrNumbers[j + 1]
arrNumbers[j + 1] = temp
print()
print("After pass:",i)
for k in range(0, n):
print(arrNumbers[k], end=' ')
print("[",i + 1, "biggest number(s) is(are) pushed to the end of the list ]")
print()
print("It was completed in",i,"passes")
print("The sorted list using Bubble Sort is: ", end=' ')
for i in range(0, n):
print(arrNumbers[i], end=' ')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With