单击Python gui按钮时退出

时间:2019-10-01 10:11:29

标签: python pyqt5

我想通过单击按钮显示结果,但是如果我按此代码,程序将在两秒钟后结束。 “ pursent.ui”只是一个尚未设置的小部件。

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic


form_class = uic.loadUiType("pursent.ui")[0]

class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.btneve)
    def btneve(self):
        self.statusbar.showMessage((int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()

1 个答案:

答案 0 :(得分:0)

  

void QStatusBar :: showMessage(const QString &message,int timeout = 0)

     

隐藏正常状态指示,并在指定的毫秒数(超时)内显示给定的消息。

import sys
from PyQt5.QtWidgets import *
#from PyQt5 import uic


#form_class = uic.loadUiType("pursent.ui")[0]

class MyWindow(QMainWindow): #, form_class):
    def __init__(self):
        super().__init__()
#        self.setupUi(self)

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)  

        self.lineEdit = QLineEdit()
        self.lineEdit_2 = QLineEdit()

        self.pushButton = QPushButton('Button')
        self.pushButton.clicked.connect(self.btneve)

        self.statusbar = self.statusBar() # = StatusBar(self)

        grid = QGridLayout(centralWidget)
        grid.addWidget(self.lineEdit)  
        grid.addWidget(self.lineEdit_2)
        grid.addWidget(self.pushButton)        


    def btneve(self):
        self.statusbar.showMessage(str(                          # + str
            (int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)
        )

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()

enter image description here