2012-03-29 3 views
0

나는 파이썬으로 작업 중이다. Qtdesigner에서 Gui.Py 모듈 GUI를 만들었습니다. 내가 별도로 만든 코드 모듈이 있습니다. 이제 문제가 있습니다. 코드 모듈에 while 루프를 잠깐 동안 인쇄하는 메서드가 있습니다. 내가 실시간으로이 작업을 수행 할 수있는 방법 .. 내 GUI 버튼 클릭 이벤트에서 해당 인쇄 메시지를 표시 textbrowser합니다 .. 샘플 코드는 다음과 같습니다GUI 모듈과 파이썬의 백엔드 코드 모듈의 통합

Gui..py 지금 샘플을

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    _fromUtf8 = lambda s: s 

class Ui_Form(object): 
def setupUi(self, Form): 
    Form.setObjectName(_fromUtf8("Form")) 
    Form.resize(400, 211) 
    self.textBrowser = QtGui.QTextBrowser(Form) 
    self.textBrowser.setGeometry(QtCore.QRect(140, 10, 256, 192)) 
    self.textBrowser.setObjectName(_fromUtf8("textBrowser")) 
    self.pushButton = QtGui.QPushButton(Form) 
    self.pushButton.setGeometry(QtCore.QRect(20, 80, 97, 27)) 
    self.pushButton.setObjectName(_fromUtf8("pushButton")) 

    self.retranslateUi(Form) 
    QtCore.QMetaObject.connectSlotsByName(Form) 

def retranslateUi(self, Form): 
    Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) 
    self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8)) 


if __name__ == "__main__": 
import sys 
app = QtGui.QApplication(sys.argv) 
Form = QtGui.QWidget() 
ui = Ui_Form() 
ui.setupUi(Form) 
Form.show() 
sys.exit(app.exec_()) 

파일. 평

import time 
class A: 
def somefunction(self): 
    i=0 
    while i<100: 
     print str(i) 
     i+=1 
     time.sleep(2) 

if __name__=='__main__': 
p=A() 
p.somefunction() 

는 제발 도와주세요, 는 근래, 대신 somefunction를 가진 결과를 직접 인쇄하는 당신에게

답변

0

감사 e yield 결과를 호출자에게 보냅니다. 호출자는 문자열을 GUI에 하나씩 추가 할 수 있습니다.

그러나 임의의 sleep 함수가 던져지는 것과 같이 결과가 생성되는 데 시간이 오래 걸리면 대부분의 GUI가 이벤트에 응답하는 데 필요한 메시지 루프를 차단하게됩니다. 이 경우 다른 스레드로 해당 작업을 오프로드하여 결과를 다시 GUI 스레드로 전달하여 표시 할 수 있습니다.

0

QTextBrowser에 print 문을 다시 표시하려면 일부 슬롯과 신호를 연결해야합니다. QtDesigner에서이 작업을 수행 할 수 있습니다.

그런 경우라면 QTimer을 봐야합니다.

p = A() 
timer = QTimer() #Form as parent?? 
timer.timeout.connect(p.somefunc) #different somefunc without the sleep thing 
timer.start(2000) # actually starts on app.exec_() 
+0

감사합니다. –