Python进度条选择限制

Python progress bar selecting limitation

我正在使用带进度条的 Tkinter。

我得到了下面的代码,其中包含 “回调”函数,可将进度条增加 50%。 我想限制该功能对每个 OptionMenu 选择只工作一次

目前,我可以在第一个 OptionMenu 上单击两次并在进度条中达到 100%。 有谁知道我应该在“回调”函数中更改什么以使其对每个 OptionMenu 只工作一次?无论用户点击了多少次来改变其选择的值。

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 函数(如果您从未使用过它们可能会造成混淆)。

这对我有用:

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'}

budgetset = False
commitmentset = False
def callback(nb):
    global budgetset, commitmentset
    if nb == 0 and not budgetset:
        budgetset = True
        progress["value"] += 50
    if nb == 1 and not commitmentset:
        commitmentset = True
        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", lambda *_, x=0: callback(x))

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", lambda *_, x=1: callback(x))

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()

试试这个:

from tkinter import *
from tkinter.ttk import Progressbar

def callback(*args):
    user_input = (var_1.get(), var_2.get()) # Here you can add even more variables
    value = 100 - 100/len(user_input)*(user_input.count("")+user_input.count("Select option"))
    progress.config(value=value)

root = Tk()

progress = Progressbar(root, orient="horizontal", length=300)
progress.pack()

var_1 = StringVar(root)
var_1.trace("w", callback)

optionmenu_1 = OptionMenu(root, var_1, "Select option", "Option 1", "Option 2")
optionmenu_1.pack()

var_2 = StringVar(root)
var_2.trace("w", callback)

optionmenu_2 = OptionMenu(root, var_2, "Select option", "Option 1", "Option 2")
optionmenu_2.pack()

# You can remove these if you don't like them:
var_1.set("Select option")
var_2.set("Select option")

root.mainloop()

它计算空 OptionMenus 的数量并将进度条设置为正确的百分比。