python-appjar函数没有';t形螺纹

python-appjar函数没有';t形螺纹,python,multithreading,user-interface,python-3.5,python-multithreading,Python,Multithreading,User Interface,Python 3.5,Python Multithreading,在尝试使用Aweasome在python中运行线程时遇到问题 下面的程序需要通过列表计数,同时更新进度条。我遵循了,但它返回了NameError:name“完成百分比”未在app.thread(第35行)中定义,您要在其中插入函数参数-我的代码如下: from appJar import gui import time # define method the counts through a list of numbers, and updates the progress meter de

在尝试使用Aweasome在python中运行线程时遇到问题

下面的程序需要通过列表计数,同时更新进度条。我遵循了,但它返回了
NameError:name“完成百分比”未在
app.thread
(第35行)中定义
,您要在其中插入函数参数-我的代码如下:

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()
我可以通过如下定义
完成百分比来消除错误:

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加载并按下按钮时,它不会执行线程。相反,它遍历列表,然后更新进度条

有没有人遇到过同样的问题?任何洞察都将不胜感激!
谢谢

这里有几个问题:

  • 首先,我不确定你的数学成绩是否有好的百分比来更新仪表,因此你可能看不到太多的变化-你应该使用
    I

  • 其次,直到循环(以及其中的睡眠)全部完成,GUI才会更新。相反,您应该尝试计算要处理的项的数量,并使用
    after()
    函数迭代这些项,请参见此处:

  • 第三,最后对
    app.thread()
    的调用没有什么效果-它调用了
    update\u meter()
    函数,但参数不存在,可以删除

  • 第四,实际的
    update\u 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)
当前对象=0
def压力(btn):
全局当前对象
当前对象=0
进程列表()
def processList():
全局当前对象
对象=[1,3,6]
总计=长度(对象)
如果当前对象<总计:
i=对象[当前对象]
印刷品(一)
当前对象+=1
当前完成百分比=(当前对象/总数)*100
应用设置表(“进度”,当前完成百分比)
应用程序后(1000,进程列表)

更新:为了澄清数学问题,您将一个整数除以另一个整数:0/3、1/3、2/3、3/3等等。在python2中,这将导致0,在python3中,您将得到分数