繁体   English   中英

如何停止功能?

[英]How do I stop a function?

因此,这是一个小游戏代码的开头。 目的是消除最接近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()
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM