python 中选项菜单的动态进度条

Dynamic progress bar for options menu in python

In Python - 如何让进度条动态化? (更改选项菜单 selected) 例如,如果我 select pl1(选择列表)中的一个选项,进度条将会改变(假设进度为 20%)。

到目前为止我试过:

from tkinter import *
from tkinter.ttk import Progressbar

pets= {'Cat', 'Dog', 'Fish'}

def __init__(self):

    root =Tk()
    root.title('window')
    root.geometry('1300x690')
    root.resizable(False, False)

    progress = Progressbar(root, orient=HORIZONTAL, length=300, mode='determinate')
    progress.place(x=500, y=15)

    var1 = StringVar(root)
    pl1 = OptionMenu(root, var1, *self.pets)

    pl1.config(width=20, bg="GREEN", fg="white")
    pl1.place(x=470, y=230)

    #Here I want to add 20% progress to the bar if var1 has been selected (no matter what is the 
    value).

    root.mainloop()

谢谢!

尝试这样的事情:

from tkinter.ttk import Progressbar
import tkinter as tk


def callback(*args):
    # Increment the progressbar's value by 20%
    progressbar["value"] += 20

root = tk.Tk()

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

var = tk.StringVar(root)
pl1 = tk.OptionMenu(root, var, *(1, 2, 3, 4, 5))
pl1.pack()

# Whenever the value of `var` is changed call callback
var.trace("w", callback)

root.mainloop()

我基本上跟踪 var 的值,当它发生变化时 callback 被调用。当调用 callback 时,它会传递一些我们可以忽略的参数。