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

2024-09-30 01:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中-如何使进度条动态?(如果选择了“选项”菜单,则会发生更改) 例如,如果我在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()

谢谢


Tags: to进度条fromimportselffalsetkinter选项
1条回答
网友
1楼 · 发布于 2024-09-30 01:24:02

试着这样做:

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时,它会传入一些我们可以忽略的参数

相关问题 更多 >

    热门问题