Tkinter Link 带进度条的按钮

Tkinter Link Button With Progress Bar

我正在使用 Tkinter 创建一个 cookie Clicker 应用程序。我想在我的程序中有一个进度条,每次单击 cookie 100 次时它都会填满。我使用 import ttk 创建了进度条,但是我不知道如何在按下按钮时更新进度条。 到目前为止,这是我的代码:

from Tkinter import *
from Tkinter import Canvas
import ttk
window1 = Tk()
window1.title("Cookie Clicker")
window1.config(background="dodger blue")
window1.geometry("254x370")
clicks = 0


def cookie_clicks():
    global clicks
    clicks = clicks + 1
    print("{0}".format(clicks))
    if clicks == 1:
        lbl1.configure(text="{0} Cookie!".format(clicks))
    else:
        lbl1.configure(text="{0} Cookies!".format(clicks))


cookie = Button(window1, highlightbackground="dodger blue", borderwidth=0, cursor="hand2", command=cookie_clicks)
photo = PhotoImage(file="imageedit_3_3213999137.gif")
cookie.config(image=photo, width="250", height="250")
cookie.place(x=0, y=90)
w = Canvas(window1, width=254, height=75, highlightbackground="gray")
w.pack()
w.create_rectangle(10, 10, 80, 80, outline="gray", fill="gray", width=100000)
w2 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w2.place(x=0, y=85)
w3 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w3.place(x=249, y=85)
w4 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w4.place(x=0, y=88)
w5 = Canvas(window1, width=250, height=0.1, highlightbackground="dodger blue")
w5.place(x=0, y=338)
lbl1 = Label(window1, bg="gray", fg="dodger blue", text="{0} Cookies!".format(clicks), font=("kavoon", 20))
lbl1.place(x=75, y=45)
lbl2 = Label(window1, bg="gray", fg="dodger blue", text="Cookie Clicker", font=("kavoon", 30))
lbl2.place(x=20, y=0)
pb = ttk.Progressbar(window1, orient='horizontal', mode='determinate')
pb.pack()


window1.mainloop()

如有任何帮助,我们将不胜感激!

无需过多更改代码,您可以执行以下操作:

def cookie_clicks():
    global clicks, pb
    clicks = clicks + 1
    pb.step(1)
    print("{0}".format(clicks))
    if clicks == 1:
        lbl1.configure(text="{0} Cookie!".format(clicks))
    else:
        lbl1.configure(text="{0} Cookies!".format(clicks))

使用global变量不是很好。您应该考虑将您的应用程序封装在 class.