2016-10-25 14 views
0

エラーメッセージ1(コードのコメントを参照)を呼び出すと、メッセージがすぐに表示されて消えます。しかし、エラーメッセージ2を呼び出すと、「OK」ボタンをクリックすると表示され、消えるだけです。PyQt5:起動後にQMessageBoxが消える

エラーメッセージ1がエラーメッセージ2のように機能するように修正するにはどうすればよいですか?

try: 
     connection = pymysql.connect(host = 'localhost', 
      user = 'root', 
      db = 'Telephon Register', 
      cursorclass = pymysql.cursors.DictCursor) 
     cur = connection.cursor() 

     if number!= "": 
      cur.execute("SELECT Number FROM formen WHERE Telephonebook = " + self.number.text()) 
      result = cur.fetchone() 

      if len(result) == 0: 
       cur.execute("INSERT INTO formen VALUES(" + self.number.text()) 
       connection.commit() 
      else: 
       print("The number " + number+ " already exists.") 
     else: 
      print("You have not typed a number!") 
      msg = QMessageBox() #EXCEPTION MESSAGE ONE 
      msg.setIcon(2) 
      msg.setText("Some Text") 
      msg.setInformativeText("Some informative text") 
      msg.setWindowTitle("Error") 
      msg.show() 

     connection.close() 
    except: 
     print("Connection does not work!") 
     msg = QMessageBox()  # EXCEPTION MESSAGE TWO 
     msg.setIcon(3) 
     msg.setText("Some Text") 
     msg.setInformativeText("Some message") 
     msg.setWindowTitle("Error") 
     msg.show() 

答えて

3

メッセージボックスは、参照を保持していないため消滅し、関数が返されるとすぐにガベージコレクションされます。

msg = QMessageBox() 
... 
msg.exec_() 
+0

グレートを:

は、彼らがブロックされるように、ユーザがそれらを閉じるまで、execを使用してメッセージボックスを開いて、あなたの例でこれを修正するには!私は今までそれを知らなかった。 :) ありがとうございました! –

関連する問題