0

스레드를 파이썬에서 쓰레드를 얻으려는 데 문제가있어서 Appjar package을 사용하지 않습니다.파이썬 appjar 함수는

다음 프로그램은 목록을 통해 계산하고 진행률 표시 줄을 동시에 업데이트해야합니다. 나는 appjar documentation for threading를 따라했지만, 당신이 기능 PARAMS를 삽입하는 것을 의미하고있는 app.thread (라인 35)에 NameError: name 'percent_complete' is not defined을 반환하는 것 - 내 코드는 다음과 같습니다 : 나는하여 오류를 제거 할 수

from appJar import gui 
import time 

# define method the counts through a list of numbers, and updates the progress meter 

def press(btn): 
    objects = [1,3,6] 
    total = len(objects) 
    current_object = 0 
    for i in objects: 
     print(i) 
     current_object += 1 
     current_percent_complete = (current_object/total) * 100 
     updateMeter(current_percent_complete) 
     time.sleep(1) 

def updateMeter(percent_complete): 
    app.queueFunction(app.setMeter, "progress", percent_complete) 

# create a GUI variable called app 

app = gui("Login Window") 
app.setBg("orange") 
app.setFont(18) 

# add GUI elements : a label, a meter, & a button 

app.addLabel("title", "COUNTER") 
app.setLabelBg("title", "blue") 
app.setLabelFg("title", "orange") 

app.addMeter("progress") 
app.setMeterFill("progress", "green") 

app.addButton("START COUNTING", press) 

# put the updateMeter function in its own thread 

app.thread(updateMeter, percent_complete) 

# start the GUI 

app.go() 

과 같이 percent_complete 정의 : GUI로드와 버튼을 누를 때

from appJar import gui 
import time 

# define method the counts through a list of numbers, and updates the progress meter 

percent_complete = 0 

def press(btn): 
... 

그러나, 그것은 스레드하지 않습니다. 대신 목록을 반복하고 나중에 진행률 막대를 업데이트합니다.

같은 문제가있는 사람이 있습니까? 어떤 통찰력이라도 대단히 감사 할 것입니다! 감사합니다.

답변

0

여기에 문제의 몇 가지가 있습니다 : 나는 당신의 수학은 당신이 많은 변화를 볼 수 있도록로 미터를 업데이트하는 좋은 비율을 초래할 확실하지 않다,

  • 첫째 - 당신이 사용되어야한다 i?

  • 둘째, 루프가 완료 될 때까지 GUI가 업데이트되지 않습니다. 대신,이 과정에 얼마나 많은 항목을 계산하려고하고 after() 기능들을 통해 반복해야합니다 여기를 참조 : http://appjar.info/pythonLoopsAndSleeps/#conditional-loops

  • 셋째, 마지막에 app.thread()에 대한 호출이 훨씬 달성하지 않습니다 - 그것은 update_meter() 함수를 호출 매개 변수가 없으면 제거 할 수 있습니다. 일단 당신이 사용 해보세요도 제거 할 수 있습니다 ...

'- 당신이 정말로 스레드를 사용하지 않는로

  • 넷째, 실제 update_meter() 기능은 필요하지 않습니다 수학을 살펴 했어 :

    current_object = 0 
    def press(btn): 
        global current_object 
        current_object = 0 
        processList() 
    
    def processList(): 
        global current_object 
        objects = [1,3,6] 
        total = len(objects) 
        if current_object < total: 
         i = objects[current_object] 
         print(i) 
         current_object += 1 
         current_percent_complete = (current_object/total) * 100 
         app.setMeter("progress", current_percent_complete) 
         app.after(1000, processList) 
    

    UPDATE : 그냥 수학 문제에 대해 명확하게, 다른 하나의 정수를 나누어있어 : 0/3, 1/3, 2/3, 3/3 등등. python2에서는 0이되고, python3에서는 분수를 얻습니다.