gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:52:22 28 4
gpt4 key购买 nike

我需要帮助在我的 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_())

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

算法.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。要做到这一点,这是语法:import filename您可以忽略 .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_())

算法.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=' ')

关于python - 如何单击 PyQt5 代码上的按钮并允许它执行/运行另一个 .py 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43011228/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com