【问题标题】:How do I stop a function?如何停止功能?
【发布时间】:2015-11-13 20:22:34
【问题描述】:

所以,这里是一个小游戏的代码的开始。目的是化解最接近0的3个炸弹。我已经成功地创建了窗口,进行了倒计时......但我不知道如何设置一个按钮来停止倒计时。如果有人可以帮助我,我已经有好几天在寻找解决方案了:(

import time, os
from Tkinter import *
from math import *
import random

#####################################################
def new():
    fenetre.destroy()

我们将专注于那部分

    def decompte(label, count=10):
        label.config(text="{:.2f}".format(count))
        if count > 0 :
            fen1.after(10,decompte, *(label, count-0.01))

   def stopper():

    fen1=Tk()
    fen1.geometry("500x500")
    lab=Label(fen1, text="")
    lab.pack()
    lab1=Label(fen1, text="")
    lab1.pack()
    lab2=Label(fen1, text="")
    lab2.pack()
    lab3=Label(fen1,text="")
    lab3.place(x=300,y=200)

    x=(random.randint(3,4))
    y=(random.randint(5,6))
    z=(random.randint(6,7))

    decompte(lab, x)
    decompte(lab2, y)
    decompte(lab3,z)

    btn1=Button(fen1, text="stop", command=stopper)
    btn1.pack()
    btn3=Button(fen1, text="quit",command=fen1.destroy)
    btn3.pack()

    lab.place(x=100,y=200)
    lab1.place(x=150,y=200)
    lab2.place(x=200,y=200)

    fen1.mainloop()

######################################################

fenetre= Tk()
fenetre.configure(bg="black")
fenetre.geometry("1024x620")
canvas= Canvas(fenetre, width=1024, height=620,bg="black")
canvas.grid(row=0, column=0)
photo = PhotoImage(file="demineur.gif")
canvas.create_image(512,310, image=photo)
btn= Button(fenetre, text="JOUER",bg="yellow",command=new)
btn.place(x=240,y=500)
fenetre.mainloop()

【问题讨论】:

  • if count > 0 and not cancelled: 然后只需制作一个全局取消标志,当您想停止时将其设置为 true

标签: python function tkinter tkinter-canvas


【解决方案1】:
countdown = True

def decompte(label, count=10):
    label.config(text="{:.2f}".format(count))
    if count > 0 and countdown: # <- global variable `countdown`
        fen1.after(10,decompte, *(label, count-0.01))

def stopper():
    global countdown
    countdown = False

【讨论】:

    猜你喜欢
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    相关资源
    最近更新 更多