.after() 导致 tkinter 窗口冻结



我正在用tkinter制作一个饼干点击游戏。我正在尝试为玩家编程一个自动点击功能,该功能每十秒点击一次饼干。我尝试使用.after()方法,但这冻结了程序。澄清一下,其他功能(例如用户单击cookie(在自动单击在后台运行时仍需要正常运行。

这是自动点击功能:

def autoclick():
        def tensecs():
            while True:
                global clicks
                clicks = clicks + 1
                if clicks == 1:
                    lbl1.configure(text="{0} Cookie!".format(clicks))
                else:
                    lbl1.configure(text="{0} Cookies!".format(clicks))
        shop_window.after(1000, tensecs)

这是完整的程序:

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

class FlashableLabel(Label):
    def flash(self, count):
        bg = self.cget('background')
        fg = self.cget('foreground')
        self.configure(background=fg, foreground=bg)
        count += 1
        if count < 1000:
            self.after(250, self.flash, count)

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

def about_btn():
    about_window = Toplevel(window1)
    about_window.overrideredirect(1)
    about_window.geometry("230x150+15+50")
    about_window.config(background="snow2")
    photo4 = PhotoImage(file="imageedit_4_8477195169.gif")
    photolbl = Label(about_window, image=photo4, borderwidth=0)
    photolbl.image = photo4
    photolbl.pack()
    lbl3 = Label(about_window, text="Cookie Clicker", bg="snow2", fg="black", font=("Lucida Grande bold", 14))
    lbl3.pack()
    lbl4 = Label(about_window, text="Version 13.0.4 (24502.5.6)", bg="snow2", fg="black", font=("Lucida Grande", 9))
    lbl4.pack()
    copyright_symb = u'N{COPYRIGHT SIGN}'.encode('utf-8')
    lbl5 = Label(about_window, text="Copyright {0} 2003-2018 Hatna Inc.nAll rights reserved.".format(copyright_symb), bg="snow2", fg="black", font=("Lucida Grande", 10))
    lbl5.pack()

def shop_btn():
    def autoclick():
        def tensecs():
            while True:
                global clicks
                clicks = clicks + 1
                if clicks == 1:
                    lbl1.configure(text="{0} Cookie!".format(clicks))
                else:
                    lbl1.configure(text="{0} Cookies!".format(clicks))
        shop_window.after(1000, tensecs)
    def double():
        global clicks
        if clicks >= 20:
            clicks = clicks * 2
            clicks = clicks - 20
            if clicks == 1:
                lbl1.configure(text="{0} Cookie!".format(clicks))
            else:
                lbl1.configure(text="{0} Cookies!".format(clicks))
            shop_window.destroy()
            global lbl3
            lbl3.configure(text="", fg="dodger blue")
        else:
            shop_window.destroy()
            lbl3.configure(text="You Need More Cookies!", fg="snow2")
    shop_window = Toplevel(window1)
    shop_window.overrideredirect(1)
    shop_window.geometry("254x310+5+50")
    frame = Frame(shop_window, width=254, height=310)
    frame.pack()
    canvas = Canvas(frame, width=254, height=310, bg="dodger blue", bd=0, highlightthickness=0, relief="ridge", scrollregion=(0, 0, 500, 500))
    vbar = Scrollbar(frame, orient=VERTICAL)
    vbar.pack(side=RIGHT, fill=Y)
    vbar.config(command=canvas.yview)
    canvas.config(yscrollcommand=vbar.set)
    canvas.pack()
    canvas.create_rectangle(0, 0, 254, 40, outline="snow2", fill="snow2")
    lbl3 = canvas.create_text(135, 20, text="{0} Cookies".format(clicks), fill="dodger blue", font=("kavoon", 15))
    global photo5
    photo5 = PhotoImage(file="imageedit_1_6195264074.gif")
    photolbl = canvas.create_image(20, 20, image=photo5)
    x2 = canvas.create_text(35, 120, text="X2", fill="gray", font=("arial bold", 40))
    lbl4 = canvas.create_text(115, 110, text="Double Your Score", fill="black", font=("arial bold", 12))
    lbl5 = canvas.create_text(94, 129, text="20 Cookies", fill="black", font=("arial", 12))
    buybtn1 = Button(canvas, text="Buy", highlightbackground="dodger blue", command=double)
    buybtn1_window = canvas.create_window(210, 119.5, window=buybtn1)
    global photo6
    photo6 = PhotoImage(file="imageedit_24_7373388957.gif")
    photolbl2 = canvas.create_image(32.5, 70, image=photo6)
    lbl6 = canvas.create_text(124, 61, text="10 Second Auto-Click", fill="black", font=("arial bold", 12))
    lbl5 = canvas.create_text(94, 80, text="15 Cookies", fill="black", font=("arial", 12))
    buybtn2 = Button(canvas, text="Buy", highlightbackground="dodger blue", command=autoclick)
    buybtn2_window = canvas.create_window(210, 70, window=buybtn2)

w = Canvas(window1, width=254, height=80, highlightbackground="snow2")
w.pack()
w.create_rectangle(10, 10, 80, 80, outline="snow2", fill="snow2", width=100000)
cookie = Button(window1, highlightbackground="dodger blue", borderwidth=0, cursor="hand2")
cookie.bind('<Button-1>', cookie_clicks)
window1.bind('<Return>', cookie_clicks)
photo = PhotoImage(file="imageedit_3_3213999137.gif")
cookie.config(image=photo, width="250", height="250")
cookie.place(x=0, y=90)
w2 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w2.place(x=0, y=86)
w3 = Canvas(window1, width=0.1, height=250, highlightbackground="dodger blue")
w3.place(x=249, y=86)
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="snow2", fg="dodger blue", text="{0} Cookies!".format(clicks), font=("kavoon", 20))
lbl1.place(x=75, y=40)
lbl2 = Label(window1, bg="snow2", fg="dodger blue", text="Cookie Clicker", font=("kavoon", 30))
lbl2.place(x=20, y=0)
pb = ttk.Progressbar(window1, orient='horizontal', mode='determinate')
pb.place(x=76, y=68)
shop = Button(window1, highlightbackground="dodger blue", borderwidth=0, command=shop_btn)
photo2 = PhotoImage(file="imageedit_28_9392607524.gif")
shop.config(image=photo2, width="44", height="40")
shop.place(x=5, y=345)
w6 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w6.place(x=7, y=345)
w7 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w7.place(x=7, y=381)
w8 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w8.place(x=6, y=350)
w9 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w9.place(x=45, y=350)
info = Button(window1, highlightbackground="dodger blue", borderwidth=0, command=about_btn)
photo3 = PhotoImage(file="imageedit_48_3021448243.gif")
info.config(image=photo3, width="44", height="40")
info.place(x=49, y=345)
w10 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w10.place(x=51, y=345)
w11 = Canvas(window1, width=38, height=0.1, highlightbackground="dodger blue")
w11.place(x=51, y=381)
w12 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w12.place(x=50, y=350)
w13 = Canvas(window1, width=0.1, height=25, highlightbackground="dodger blue")
w13.place(x=90, y=350)
lbl3 = FlashableLabel(window1, bg="dodger blue", fg="dodger blue", text="", font=("kavoon", 12))
lbl3.place(x=100, y=353)
lbl3.flash(0)
window1.mainloop()

任何帮助将不胜感激! 请原谅我的 GUI 格式错误,我还没有完全开始尝试.grid

你需要使用 after() 而不是 while 循环,而不是补充。试试这个:

def autoclick():
    global clicks
    clicks = clicks + 1
    if clicks == 1:
        lbl1.configure(text="{0} Cookie!".format(clicks))
    else:
        lbl1.configure(text="{0} Cookies!".format(clicks))
    shop_window.after(1000, autoclick) # repeat this function every second

相关内容

  • 没有找到相关文章

最新更新