0

有问题尝试使用真棒Appjar package获取线程在python中的工作。python appjar函数不会线程

以下程序需要通过列表进行计数,并同时更新进度条。我跟着appjar documentation for threading,但它的返回NameError: name 'percent_complete' is not definedapp.thread(35行),在你打算插入功能PARAMS - 我的代码如下:

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像这样:

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): 
... 

然而,当GUI负载和按钮被按下不线程。相反,它遍历列表,然后更新进度条。

有没有人遇到过同样的问题?任何洞察力将非常感激! 谢谢!

回答

0

有几个问题在这里:

  • 首先,我不知道你的数学产生良好的百分比,以更新与仪表,所以你可能看不到太大的变化 - 你应该使用i

  • 其次,GUI将不会更新,直到循环(及其内部的睡眠)全部完成。相反,你应该尝试计算多少项目进程,并通过他们与after()功能迭代,在这里看到:http://appjar.info/pythonLoopsAndSleeps/#conditional-loops

  • 第三,在最后调用app.thread()没有取得太大的 - 它调用update_meter()功能用一个不存在的参数,它可以被删除。

  • 四,实际update_meter()功能是没有必要的,因为你没有真正使用线程 - 可以同时删除...

让这个尝试,一旦你” VE有一个看看数学:

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中,你会得到分数。