Py学习  »  Python

销毁标签的python tkinter

Kiyo • 5 年前 • 1506 次点击  

我知道有很多源显示了如何使用destroy命令,但出于某种原因,这会忽略destroy命令并继续创建更多的文本标签。有什么关于这是为什么的想法吗?这是我做的一个小游戏的代码。

你可以在“integrity()”下找到我要销毁的标签。

    from tkinter import *
from random import randrange
class Window(Frame):
    def position(self):
        return {"x":randrange(0,350),"y":randrange(0,250)}
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.master = master
        self.__init__window()
    def __init__window(self):
        global count
        count=0
        self.master.title("GUI")
        self.pack(fill=BOTH, expand=1)
        self.Button1 = Button(self, text="Click me",command=self.Message)
        self.Button1.place(**self.position())
        self.Button2 = Button(self, text="Click me if you can",command=self.integrity)
        self.Button2.place(**self.position())
        menu=Menu(self.master)
        self.master.config(menu=menu)
        file = Menu(menu)
        file.add_command(label="Exit", command=self.client_exit)
        menu.add_cascade(label="File",menu=file)
        edit = Menu(menu)
        edit.add_command(label="Starto", command=self.showText)
        menu.add_cascade(label="Edit", menu=edit)
    def Message(self):
        print("Hello world")
        self.Button1.place(**self.position())
    def showText(self):
        text = Label(self, text="Clicks: ")
        text.pack()
    def integrity(self):
        self.Button2.place(**self.position())
        global count
        count=count+1
        self.text1 = Label(self, text=count)
        self.text1.destroy()
        self.text1 = Label(self,text=count)
        self.text1.pack()
        print("Clicks: ",count)
        if count<5:
            print("(づòДó)づ")
            print("Dont click!")
            print("﴾´• ω •`﴿\n")
        elif count<10:
            print("ヽ(òДó)ノ")
            print("Stop it")
            print("(づòДó)づ\n")
        elif count<15:
            print("﴾⇀∀↼﴿")
            print("Stop it please!\n")
        else:
            print("Fine you win just stop! ლ(>Д<ლ)\n")
        def client_exit(self):
        exit()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39193
 
1506 次点击  
文章 [ 1 ]  |  最新文章 5 年前