2017-08-04 1 views
-1

PyQt4でプログラムを作成しようとしましたが、質問に対する回答を求めるプロンプトが表示されます。答えが正しければ、答えが正しいことを記し、新しい質問を生成しなければならない。答えが間違っている場合は、その行を印刷してから同じ質問をもう一度表示する必要があります。実施例は以下のとおりです。TextBrowserはPyQt4の接続されたプロセスを通してテキストを更新しません。

import sys 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
import numpy as np 
import time 


class Window(QWidget): 
    def __init__(self,parent=None): 
     super(Window, self).__init__(parent) 
     self.lineEdit = QLineEdit() 
     self.textBrowser = QTextBrowser() 
     layout = QVBoxLayout() 
     layout.addWidget(self.textBrowser) 
     layout.addWidget(self.lineEdit) 
     self.setLayout(layout) 
     self.lineEdit.setDisabled(False) 
     self.go_outer() 
    def go_outer(self): 
     random_instance=["home","honey","micro"] 
     random_corresponding=["depot","well","soft"] 
     random_index=np.random.randint(0,3) 
     self.ret=random_instance[random_index] 
     self.corres=random_corresponding[random_index] 
     self.go() 
    def go(self): 
     self.textBrowser.append(self.ret) 
     self.lineEdit.returnPressed.connect(self.process) 
    def process(self): 
     userInput = self.lineEdit.text() 
     if userInput == self.corres: 
      self.textBrowser.append("Your answer is correct.") 
      self.textBrowser.update() 
      time.sleep(5) 
      self.textBrowser.clear() 
      self.go_outer() 
     else: 
      self.textBrowser.append("Your answer is incorrect. Please try again.") 
      self.textBrowser.update() 
      time.sleep(5) 
      self.lineEdit.clear() 
      self.textBrowser.clear() 
      self.go() 



def run(): 
    app=QApplication(sys.argv) 
    GUI=Window() 
    GUI.show() 
    sys.exit(app.exec_()) 

if __name__ == '__main__': 
    run() 

問題は、それが外機能「()GET_TEXT」から得られたテキストを示しているが、「あなたの答えが正しい」か「あなたの答えが間違って表示されていないこと、です。もう一度やり直してください。 "

なぜこれがそうであり、その結果、どのように解決するのか?

+0

。 –

+0

質問を編集しました。 –

+0

これは実際の例かもしれませんが、最小の例からは程遠いです。エラーは、最小限の例ではるかに速く検出することができます。あなた自身でそれらを見つけることさえできるかもしれません。 – Trilarion

答えて

0

信号をスロットに一度接続するだけで、コンストラクタに移動する必要があります。

もう1つの問題は、スリープ機能がGUIに優しくないことです。この場合、タイマーを使用することをお勧めします。この場合はQTimer.singleShot()を使用し、少し変更します。

上記次のコードで実装されてすべての:それは、データベースからデータベースと対応する文字列の文字列のキーの文字列を返す

class Window(QWidget): 
    def __init__(self,parent=None): 
     super(Window, self).__init__(parent) 
     self.lineEdit = QLineEdit() 
     self.textBrowser = QTextBrowser() 
     layout = QVBoxLayout() 
     layout.addWidget(self.textBrowser) 
     layout.addWidget(self.lineEdit) 
     self.setLayout(layout) 
     self.lineEdit.setDisabled(False) 
     self.go_outer() 
     self.lineEdit.returnPressed.connect(self.process) 

    def go_outer(self): 
     random_instance=["home","honey","micro"] 
     random_corresponding=["depot","well","soft"] 
     random_index=np.random.randint(0,3) 
     self.ret=random_instance[random_index] 
     self.corres=random_corresponding[random_index] 
     self.go() 

    def go(self): 
     self.textBrowser.clear() 
     self.lineEdit.setDisabled(False) 
     self.lineEdit.clear() 
     self.textBrowser.append(self.ret) 

    def process(self): 
     userInput = self.lineEdit.text() 
     self.lineEdit.setDisabled(True) 
     if userInput == self.corres: 
      self.textBrowser.append("Your answer is correct.") 
      QTimer.singleShot(5000, self.go_outer) 
     else: 
      self.textBrowser.append("Your answer is incorrect. Please try again.") 
      QTimer.singleShot(5000, self.go) 
関連する問題