如何在PyQt5代码上单击pushButton并允许它执行/运行另一个.py文件?

时间:2017-03-25 00:39:57

标签: python qt button pyqt pyqt5

我需要帮助在我的PyQt5代码上链接pushButton,以便它自动运行另一个.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=' ')

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

由于

2 个答案:

答案 0 :(得分:2)

不确定这是否是最佳方式,但我要做的是将所有代码都放在algorithm.py中,并将其放入类似的函数中。

def main():
    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 __name__ == "__main__":
    main()

然后将代码导入到你的main.py中。

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.clicked)

    def clicked(self):
        algorithm.main()


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

希望这有帮助。

答案 1 :(得分:2)

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

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