2013-09-02 4 views
1

내장 된 Python GUI Tkinter로 프로그램을 프로토 타이핑하고 동시에 여러 라벨을 업데이트하려고 할 때 문제가 있습니다. GUI는 몇 가지 새로 고침을 처리 할 수 ​​있지만 10 초 정도 지나면 멈 춥니 다. 나는 각 레이블이 다른 센서 데이터를 표시하기 때문에 모든 레이블을 동시에 거의 동시에 업데이트해야합니다.python - 여러 라벨을 동시에 업데이트 할 때 Tkinter가 응답하지 않음

지금은 random 모듈을 사용하여 실제 데이터 대신 숫자를 생성합니다. 동시에 10 개 정도의 레이블이 있으므로, tkinter가이를 처리 할 수 ​​있는지 확인하고 싶습니다. 레이블은 초 단위로 업데이트됩니다 (약 200 밀리 초). 나는 창에서 파이썬 3.3을 사용하고있다.

몇 가지 옵션에 대해 읽었지만 tkinter에는 익숙하지 않습니다. 링크 : 나는 Here. And Here.

멀티 스레딩 고려 하는가? 다중 처리? 나는 어느 경험도 가지고 있지 않지만 이것을 해결할 뜻이 있다면 배울 의향이있다.

tkinter으로도 내가 뭘하려하고 있습니까? pygame 또는 다른 GUI를 사용해야합니까? 내가 여기서 무엇을 놓치고 있니? 내가 여기에 생각

import random 
from tkinter import * 

class Application(Frame): 
    """The GUI window""" 
    def __init__(self, master): 
     """Setup the Frame""" 
     super(Application, self).__init__(master) 
     self.grid() 
     self.setupWidgets() 
     self.update() 

    def setupWidgets(self): 
     """Setup widgets for use in GUI""" 

     #setup data readings 
     self.data_lbl_txt = "Data Readings" 
     self.data_lbl = Label(self, text = self.data_lbl_txt) 
     self.data_lbl.grid(row = 0, column = 1, columnspan = 2, sticky = E) 


     #setup first data piece 
     self.data1_txt = "First piece of data:" 
     self.data1_lbl = Label(self, text = self.data1_txt) 
     self.data1_lbl.grid(row = 1, column = 1, sticky = W) 

     self.data1 = StringVar() 
     self.data1_Var = Label(self, textvariable = self.data1) 
     self.data1_Var.grid(row = 1, column = 2, sticky = W) 

     #setup second data piece 
     self.data2_txt = "Second piece of data:" 
     self.data2_lbl = Label(self, text = self.data2_txt) 
     self.data2_lbl.grid(row = 2, column = 1, sticky = W) 

     self.data2 = StringVar() 
     self.data2_Var = Label(self, textvariable = self.data2) 
     self.data2_Var.grid(row = 2, column = 2, sticky = W) 


    def update(self): 
     self.data1.set(random.randint(1,10)) 
     self.data1_Var.after(200, self.update) 

     self.data2.set(random.randint(1,10)) 
     self.data2_Var.after(200, self.update) 



root = Tk() 
root.title("Data Output") 
root.geometry("600x250") 
window = Application(root) 


window.mainloop() 
+0

Tkinter는 10 개의 라벨 업데이트를 쉽게 처리 할 수 ​​있습니다. –

답변

3

당신은 실수로 update 방법에 fork bomb을 만들었습니다.

이 코드 :

def update(self): 
    self.data1.set(random.randint(1,10)) 
    self.data1_Var.after(200, self.update) 

    self.data2.set(random.randint(1,10)) 
    self.data2_Var.after(200, self.update) 

는 두 번 .after(200, self.update)이 같은 방법이 호출 될 때마다이,이, 미래에 200 밀리 초를 두 번 다시 호출 (또는 오히려된다는 것을 의미합니다

. 이것은 대신의 의미 :

:
update is called 1x 
200 millisecond gap 
update is called 1x 
200 millisecond gap 
update is called 1x 
200 millisecond gap 
update is called 1x 
200 millisecond gap 
update is called 1x 
200 millisecond gap... 

이 있습니다

2^50 (또는 1125899906842624) update 함수를 동시에 호출하려고하므로 약 10 초 (또는 10,000 밀리 초) 후에 멈출 것입니다!

나는 Tkinter를 사용한 이래로 얼마간의 시간이 걸렸다 고 생각합니다. 그 해결책은 그 기능에서 root.after을 대신 수행하는 것입니다. 또는 여러 개의 다른 update 함수를 만들 수 있습니다. 각 함수는 각각 after을 한 번만 호출합니다.

+0

'root.after()'를 호출해도 아무런 변화가 없지만 그것이 멈추는 이유에 대한 분석은 올바르게 수행됩니다. –

+0

('self.data1_Var.after (200, self.update)'등을'self.after (200, self.update)'로 바꾸는 것과 같다) – rlms

관련 문제