PyQt5 - 如何在使用QMessageBox进行错误处理后将应用程序返回到初始状态

时间:2017-09-27 23:51:08

标签: python-3.x pyqt5

刚开始使用Python3和PyQt5,我有点卡在这里。

我的主窗口输入两个股票代码作为输入,在用户按下Show Me后!按钮,输出每个的平均比率。我创建了一个带有OK按钮的QMessageBox,当用户输入无效的代码时,该按钮会弹出。

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import good_morning as gm

import MainUI

class MainWindow(QMainWindow, MainUI.Ui_MyStockratios):


    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.home()

    def home(self):
        #If Show Me! button is clicked, go grab_user_input()
        self.show_me_btn.clicked.connect(self.grab_user_input)

    def grab_user_input(self):

        #Grab user input for QLineEdits
        self.ticker1_value = self.ticker1_label.text()
        self.ticker2_value = self.ticker2_label.text()

        #Fetch the ratios and place them in a dataframe
        self.kr = gm.KeyRatiosDownloader()

        try:
            self.kr_frame1 = self.kr.download(self.ticker1_value)
            self.kr_frame2 = self.kr.download(self.ticker2_value)

        #Error handling
        except ValueError:
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Information)
            msg.setText("Invalid ticker code")
            msg.setInformativeText("Please verify the data you entered and try again.")
            msg.setWindowTitle("Error")
            msg.setStandardButtons(QMessageBox.Ok)
            reply = msg.exec_()
            if reply:
                self.ticker2_label.clear()
                self.ticker1_label.clear()
                self.home()

             [...]

def main():
    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()

这是我的问题:我希望应用程序在用户按下QMessageBox的OK按钮后返回其初始状态,这意味着必须清除QLineEdits并且应用程序必须等待用户输入新数据并按Show我!按钮再次。我使用clear()函数清除了QLineEdits,但似乎无法使应用程序等待新的用户输入。

提前致谢!

1 个答案:

答案 0 :(得分:0)

为了将来参考,您发布的代码有点不完整。我采取了一些自由来得到一个有效的例子。您可以忽略除按钮处理程序部分之外的大多数更改。您只需要连接一次按钮。不需要你的home()方法。

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
# import good_morning as gm

# import MainUI

class MainWindow(QMainWindow):#, MainUI.Ui_MyStockratios):


    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        # self.setupUi(self)
        layout = QVBoxLayout()
        widget = QWidget(self)
        self.setCentralWidget(widget)
        widget.setLayout(layout)

        self.show_me_btn = QPushButton('Show Me!', self)
        layout.addWidget(self.show_me_btn)
        self.ticker1_label = QLineEdit(self)
        layout.addWidget(self.ticker1_label)
        self.ticker2_label = QLineEdit(self)
        layout.addWidget(self.ticker2_label)


        # self.home()
        self.show_me_btn.clicked.connect(self.grab_user_input)

    # def home(self):
    #     #If Show Me! button is clicked, go grab_user_input()
    #     self.show_me_btn.clicked.connect(self.grab_user_input)

    def grab_user_input(self):

        #Grab user input for QLineEdits
        self.ticker1_value = self.ticker1_label.text()
        self.ticker2_value = self.ticker2_label.text()

        # #Fetch the ratios and place them in a dataframe
        # self.kr = gm.KeyRatiosDownloader()
        #
        # try:
        #     self.kr_frame1 = self.kr.download(self.ticker1_value)
        #     self.kr_frame2 = self.kr.download(self.ticker2_value)
        #
        # #Error handling
        # except ValueError:
        if 1:
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Information)
            msg.setText("Invalid ticker code")
            msg.setInformativeText("Please verify the data you entered and try again.")
            msg.setWindowTitle("Error")
            msg.setStandardButtons(QMessageBox.Ok)
            reply = msg.exec_()
            if reply:
                self.ticker2_label.clear()
                self.ticker1_label.clear()
                # self.home()

             # [...]

def main():
    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()