【问题标题】:I can not properly update my progressbar我无法正确更新我的进度条
【发布时间】:2016-11-01 19:03:43
【问题描述】:

我正在尝试在单击按钮时更新我的​​进度条,并且该功能应在命令时更新(单击提交按钮)但这不会正确发生

def progress(*args): #progressbar is set to be 50% completed
    p.step(50)
def submitted(*args): #Progressbar is set to be fully completed and states information recorded
    p.step(100)
    messagebox.showinfo("Information Submitted", "Your information has been recorded.")

def clear(*args): #Clears everything and returns gui to start of program
    l.selection_clear(0, END)
    t.delete('1.0', END)
    p.step(0)   

#Sets title and creates gui
root=Tk()

#Configures column and row settings and sets padding
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

#Creates list of countries alphabetically
countries=["Finland", "France", "Greece", "Iceland", "Spain"]

l=Listbox(mainframe, height=5)
l.grid(column=0, row=0, sticky=(N,W,E,S))


#Adds items in list to listbox
for i in countries:
    l.insert('end', i)


#Creates progessbar widget
p=ttk.Progressbar(mainframe, orient=VERTICAL, length=200, mode='determinate')
p.grid(column=2, row=0, sticky=(N,S))

#Activates progress function if something in listbox is selected
l.bind('<<ListboxSelect>>', progress)

#Submitting calls submitted function to set progressbar to 100 and statemessage box has been completed
subbttn= ttk.Button(mainframe, text="Submit", command=submitted)
subbttn.grid(column=1, row=1, sticky=(S, W, E))

clearbttn= ttk.Button(mainframe, text="Clear", command=clear)
clearbttn.grid(column=1, row=1, sticky=(N, W, E))
#Clears all inputs and returns program to how it was in the beginning

#Runs loop for gui
root.mainloop()

单击列表框中的某些内容后,进度条会更新为 50%,这很好。但是,单击提交按钮后没有任何变化。此外,如果我将其更改为 p.set(99) 进度条似乎正在减少。单击提交按钮后,我希望进度条完全填满。同样在单击清除按钮后,我希望进度条为 0,并且进度条没有变化。

【问题讨论】:

  • 这是很多代码。你能把它减少到只剩下必需品吗?
  • 好吧,本来我以为我已经把它剪掉了很多,并且很确定它是相关的,但是我刚刚编辑了它,并把它剪到了我认为出现问题的部分.
  • 它必须是最小的完整的。不要显示您认为有问题的代码,我们需要查看实际导致问题的代码。见stackoverflow.com/help/mcve
  • 我已将它尽可能地减少,包括所有可能的相关问题部分,并且代码运行起来非常容易运行和理解。

标签: python tkinter progress-bar


【解决方案1】:

p.step(100)加100得到"result modulo maximum"
所以你有"0+100 modulo 100",它给出了0。

但你可以使用p["value"] = 100


按钮Clear 不会更改进度条,因为函数t 中存在未知变量clear(),因此在执行p.step(0) 之前您会收到错误消息。

p.step(0) 不会将值设置为0 - 它会将0 添加到当前值。

【讨论】:

    【解决方案2】:

    看起来

    p=ttk.Progressbar(mainframe, orient=VERTICAL, length=200, mode='determinate')
    

    变成

    p=ttk.Progressbar(mainframe, variable=progressvar,orient=VERTICAL, length=200, mode='determinate')
    

    p.step(50)
    

    变成

    progressvar.set(50)
    

    使用变量并将其附加到您可以设置值的进度条将使一切按我想要的方式工作。任何值更改都完美完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多