2017-09-27 3 views
0

Je commence tout juste avec Python3 et PyQt5 et je suis coincé ici.PyQt5 - Comment retourner l'application à l'état initial après la gestion des erreurs avec QMessageBox

Ma fenêtre principale prend deux codes de téléscripteur en entrée et, après que l'utilisateur appuie sur le bouton Afficher! bouton, produit des moyennes de rapport pour chacun d'entre eux. J'ai créé un QMessageBox avec un bouton OK qui apparaît lorsque l'utilisateur entre des codes ticker invalides.

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

Voilà mon problème: je veux l'application de revenir à son état initial après que l'utilisateur appuie sur le bouton OK de la QMessageBox, ce qui signifie que les QLineEdits doivent être effacées et l'application doit attendre que l'utilisateur d'entrer de nouvelles données et appuyez sur le bouton Show Me! bouton à nouveau. J'ai effacé le QLineEdits avec la fonction de clear(), mais ne peux pas sembler faire attendre l'application pour la nouvelle entrée d'utilisateur.

Merci d'avance!

Répondre

0

Pour référence future, le code que vous avez posté est un peu incomplet. J'ai pris quelques libertés pour obtenir un exemple de travail. Vous pouvez ignorer la plupart des modifications à l'exception de la partie gestionnaire de boutons. Vous avez seulement besoin de connecter le bouton une fois. Votre méthode home() n'est pas nécessaire.

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