2012-05-31 3 views
1

창을 표시하는 방법 + 해당 텍스트를 인쇄 하시겠습니까? 내 while 루프가 있으면 창이 더 이상 표시되지 않습니다. Python - 창을 표시하는 방법 + 텍스트를 인쇄 하시겠습니까? 그 유일한 인쇄하지만 창을 표시하지 않는 곳

import sys 
import datetime 
import time 
from PyQt4 import QtCore, QtGui 

class Main(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
    super(Main, self).__init__(parent)  
    self.b = QtGui.QPushButton("exit", self, clicked=self.close) 
    self.c = QtGui.QLabel("Test", self) 

if __name__ == "__main__": 
    app=QtGui.QApplication(sys.argv) 
    myapp=Main() 
    myapp.show()  
    while True: 
    time.sleep(2) 
    print "Print this + Show the Window???!!!" 
    sys.exit(app.exec_()) 

시도 :

import sys 
import datetime 
import time 
from PyQt4 import QtCore, QtGui 

class Main(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
    super(Main, self).__init__(parent)  
    self.b = QtGui.QPushButton("exit", self, clicked=self.close) 
    self.c = QtGui.QLabel("Test", self) 

    def myRun(): 
    while True: 
     time.sleep(2) 
     print "Print this + Show the Window???!!!"  

if __name__ == "__main__": 
    app=QtGui.QApplication(sys.argv) 
    myapp=Main() 
    myapp.show()  

    thread = QtCore.QThread() 
    thread.run = lambda self: myRun() 
    thread.start()  
    sys.exit(app.exec_()) 

출력 :

형식 오류는() 전역 함수 myRun() 호출 정확히 1 인수 (0 주어진)

+0

가능한 중복 (http://stackoverflow.com/questions/3765384/pyqt-how-do-i-handle-loops-in-main) –

+0

@Aaron Digulla : 위의 내용을 참조하십시오. 적용 스레드가 작동하지 않습니다. – YumYumYum

답변

3

몇 가지 문제 : 1) 스레드를 올바르게 호출하거나 초기화하지 않습니다. 2) 다른 스레드가 실행 중일 때 이벤트를 계속 처리하도록 기본 스레드에 알려야합니다. 3) 레이블이 'exit'버튼 위로 마우스를 가져 가면 클릭 할 수 없습니다.

import sys 
import datetime 
import time 
from PyQt4 import QtCore, QtGui 

class Main(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
    super(Main, self).__init__(parent)  
    self.b = QtGui.QPushButton("exit", self, clicked=self.close) 

    def myRun(self): 
    while True: 
     time.sleep(2) 
     print "Print this + Show the Window???!!!"  

if __name__ == "__main__": 
    app=QtGui.QApplication(sys.argv) 
    myapp=Main() 
    myapp.show()  

    thread = QtCore.QThread() 
    thread.run = lambda myapp=myapp: myapp.myRun() 
    thread.start()  

    app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()")) 

    sys.exit(app.exec_()) 

    while thread.isAlive(): 
    #Make sure the rest of the GUI is responsive 
    app.processEvents() 
[PyQt는? : 나는 주에서 루프를 처리 할 방법]의
+0

고맙습니다. – YumYumYum

+0

레이블은 괜찮습니다. 마우스를 클릭 할 수 없도록 사용했지만 명시 적으로 키보드 '스페이스 바'를 사용해야합니다. – YumYumYum

+0

정확히이 행은 무엇을합니까? app.connect (app, QtCore.SIGNAL ("lastWindowClosed()"), app, QtCore.SLOT ("quit()"))'그 라인의 lastWindowClosed() 및 quit()은 어디에 있습니까? – YumYumYum

1

lambda self: myRun() 시도 걸린다. 시도해보십시오

lambda myapp=myapp: myapp.myRun() 

대신에. 이상한 할당은 thread.run()이 하나를 얻지 못하므로 기본 매개 변수를 생성합니다.

+0

여전히'Traceback (최근 호출 마지막) : 파일 "/var/tmp/python-hack-the-mac/src/Test.py", 라인 23, thread.run = lambda myapp = myapp : myapp.myRun() TypeError : myRun()은 인수가 없습니다. ' – YumYumYum

+0

'def myRun (self) : –

+0

이어야합니다. 설명했던대로 작동합니다. 고맙습니다. – YumYumYum

관련 문제