我如何在我的PyQt5代码上单击按钮并允许其执行/运行另一个.py文件?

我需要在PyQt5代码上链接按钮的帮助,以便它可以自动运行另一个.py文件.

我尝试使用’self.pushButton.clicked.connect’,但这似乎不适用于PyQt5代码.我已经在下面附加了两个我的.py文件的代码:

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_())

上面的代码应产生一个带有单个按钮的窗口.单击该按钮时,它应执行/运行以下代码

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

请您帮我解决这个问题.我一直在寻找解决方案,但似乎没有任何效果.

谢谢

最佳答案
main.py

步骤1:您需要导入algorithm.py.为此,这是语法:导入文件名,您可以忽略.py

步骤2:将按钮连接到将调用algorithm.py中的进程的函数.这是语法:self.pushButton.clicked.connect(self.FuncName)

第3步:调用algorithm.py中的函数即可完成工作.语法: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(我为此文件添加了功能FunctionAlgo(),因为它更合适并且会更容易)

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=' ')
点击查看更多相关文章

转载注明原文:我如何在我的PyQt5代码上单击按钮并允许其执行/运行另一个.py文件? - 乐贴网