我正在使用带有进度条的Tkinter
下面的代码带有“回调”函数,它为进度条增加了50%。 我想将每个选项菜单选择的功能限制为只工作一次
目前,我可以在第一个选项菜单上单击两次,并在进度栏中达到100%。 有人知道我应该在“callback”函数中更改什么,以便使它在每个选项菜单中只工作一次吗?无论用户单击多少次以更改其选定值
from tkinter import *
from tkinter.ttk import Progressbar
root = Tk()
root.title('Input window V1')
root.geometry('600x400')
root.resizable(False, False)
frame = Frame(root, width=600, height=400)
frame.configure(background="gray28")
frame.pack(fill=BOTH, expand=True)
progress = Progressbar(root, orient=HORIZONTAL, length=300, mode='determinate')
progress.place(x=150, y=15)
Budget = {'Flexible', 'Variable', 'Fixed'}
Commitment = {'High', 'Medium', 'Low'}
def callback(*args):
progress["value"] += 50
bottom_header = Label(root, bg="gray28", fg="white", pady=3,
font=("Helvetica", 20, 'underline'), text='Please fill the following attributes:')
bottom_header.place(x=110, y=100)
lbl1 = Label(root, bg="gray28", text='Budget:', fg="cyan2", font=("Helvetica", 14))
lbl1.place(x=120, y=200)
lbl2 = Label(root, bg="gray28", text='Commitment:', fg="cyan2", font=("Helvetica", 14))
lbl2.place(x=120, y=240)
var1 = StringVar(root)
pl1 = OptionMenu(root, var1, *Budget)
pl1.config(width=20, bg="GREEN", fg="white")
pl1.place(x=250, y=200)
var1.trace("w", callback)
var2 = StringVar(root)
pl2 = OptionMenu(root, var2, *Commitment)
pl2.config(width=20, bg="GREEN", fg="white")
pl2.place(x=250, y=240)
var2.trace("w", callback)
global var_dict
var_dict = dict(Budget=var1,
Commitment=var2)
button1 = Button(root, text="Test")
button1.config(width=25, bg="white")
button1.place(x=220, y=320)
root.mainloop()
提前谢谢
跟踪您已经考虑的两个因素中的哪一个,并且仅当此部分尚未添加50%时才进行更新。 回调函数被更改(并且是可以理解的),回调的传递被更改为lambda函数(如果您从未使用过它们,这可能会造成混淆)
这对我很有用:
试试这个:
它计算空
OptionMenu
的数量,并将进度条设置为正确的百分比相关问题 更多 >
编程相关推荐