2017-09-27 9 views
0

Python3とPyQt5から始めて、ちょっとここに詰まっています。PyQt5 - QMessageBoxでエラー処理後にアプリケーションを初期状態に戻す方法

メインウィンドウに2つのティッカーコードが入力され、ユーザーが[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() 

は私の問題だ:私は、アプリケーションがその初期状態に戻したいユーザーはQLineEditsを意味QMessageBoxのOKボタンを、クリアする必要があり、アプリケーションが新しいデータを入力するユーザーを待たなければなりません押した後私を見せて!ボタンをもう一度押します。 clear()関数を使ってQLineEditsをクリアしましたが、新しいユーザー入力を待つアプリケーションを作成できないようです。

ありがとうございます!

答えて

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() 
関連する問題