首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >tkinter root.destroy()而不杀死整个窗口

tkinter root.destroy()而不杀死整个窗口
EN

Stack Overflow用户
提问于 2022-05-05 18:40:28
回答 1查看 25关注 0票数 0

我正在为练习重新创建游戏字字

我正在试图找到一个函数,允许我销毁我的tkinter窗口中的所有小部件。

root.destroy()不是一个解决方案,因为它只是关闭整个窗口。

我想使用这个功能,每当我们赢了游戏,重新启动整个程序,而不必手动完成它。

程序:

代码语言:javascript
复制
from tkinter import *
from tkinter import ttk
import tkinter as tk
from english_words import english_words_alpha_set
import time

guess = "bored"
guess = guess.upper()
word=""
a=0
def callback1(var, index, mode):
    root.entry2.configure(state="normal")
    root.entry2.focus()
    root.entry1.configure(state="disabled")
    root.hasfocus = "entry2"

def callback2(var, index, mode):
    root.entry3.configure(state="normal")
    root.entry3.focus()
    root.entry2.configure(state="disabled")
    root.hasfocus = "entry3"

def callback3(var, index, mode):
    root.entry4.configure(state="normal")
    root.entry4.focus()
    root.entry3.configure(state="disabled")
    root.hasfocus = "entry4"

def callback4(var, index, mode):
    root.entry5.configure(state="normal")
    root.entry5.focus()
    root.entry4.configure(state="disabled")
    root.hasfocus = "entry5"

def callback5(var, index, mode):
    root.entry5.configure(state="disabled")
    root.hasfocus = "none"

def newRow():
    boxes = [root.box1, root.box2, root.box3, root.box4, root.box5]
    
    word = root.name1.get() + root.name2.get() + root.name3.get() + root.name4.get() + root.name5.get()
    word=word.upper()

    root.greenBoxes=0

    def checkBg():
        if word[a] == guess[a]:
            root.greenBoxes=root.greenBoxes+1
            print("greenBoxes=", root.greenBoxes)
            return '#538D4E'
        elif word[a] in guess:
            return '#B59F3B'
        else:
            return '#3A3A3C'
        
    root.entry1.destroy()
    root.entry2.destroy()
    root.entry3.destroy()
    root.entry4.destroy()
    root.entry5.destroy()
    
    a=0
    while a < 5:
        boxes[a].configure(text=word[a], bg=checkBg(), fg = "white")
        a=a+1
    
    if root.greenBoxes == 5: #if player won:
        root.WinMsg = Label(root, height=2, width=28, bg='#2d2d2f', fg='white', text='You\'ve won! The word was \"' + guess[0] + guess[1:5].lower() +"\"", font="Calibri 20")
        root.WinMsg.place(x=65, y=100) #say that he won
        #*destroy all widgets*
        root.CreateWindow() #remake all widgets (to start a new game)
        return

    root.rows = root.rows+1
    if root.rows < 6: #if player still have any try
        root.d=root.d+70
        root.createBoxes()
        root.createEntry()
    else: #if player lost
        root.GameOverMsg = Label(root, height=2, width=28, bg='#2d2d2f', fg='white', text='You\'ve lost! The word was \"' + guess[0] + guess[1:5].lower() +"\"", font="Calibri 20")
        root.GameOverMsg.place(x=65, y=100) #says that he lost
        #*destroy all widgets*
        root.CreateWindow() #remake all widgets (to start a new game)
        return


    

class root(Tk):
    def __init__(self):
        super(root, self).__init__()
        
        self.title("Wordle")
        self.minsize(580,465)
        self.windowBG = '#121213'

        self.CreateWindow()

    def CreateWindow(self):
        self.configure(bg=self.windowBG)

        self.hasfocus = ""
        self.d=0
        self.rows=0
        self.greenBoxes = 0

        self.createBoxes()
        self.createEntry()
        self.backspace = Button(self, text="Backspace", width=8, font ="Calibri 18", command=self.do_backspace)
        self.backspace.place(x=465, y=62)

        self.enter = Button(self, text="Enter", width=6, font ="Calibri 18", command=self.do_enter)
        self.enter.place(x=480, y=125)


    def createBoxes(self):
        self.box1 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
        self.box1.place(x=70, y=30+self.d)
        self.box2 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
        self.box2.place(x=150, y=30+self.d)
        self.box3 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
        self.box3.place(x=230, y=30+self.d)
        self.box4 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
        self.box4.place(x=310, y=30+self.d)
        self.box5 = tk.Label(self, text="", font="Calibri", height=2, width=6, bg="#3A3A3C")
        self.box5.place(x=390, y=30+self.d)


    def do_backspace(self):
        if self.hasfocus == "entry1":
            pass
        elif self.hasfocus == "entry2":
            self.entry1.configure(state="normal")
            self.entry1.delete(0, 'end')
            self.entry1.configure(state="normal")
            self.entry1.focus()
            self.hasfocus="entry1"
            self.entry2.configure(state="disabled")
        elif self.hasfocus == "entry3":
            self.entry2.configure(state="normal")
            self.entry2.delete(0, 'end')
            self.entry2.configure(state="normal")
            self.entry2.focus()
            self.hasfocus="entry2"
            self.entry3.configure(state="disabled")
        elif self.hasfocus == "entry4":
            self.entry3.configure(state="normal")
            self.entry3.delete(0, 'end')
            self.entry3.configure(state="normal")
            self.entry3.focus()
            self.hasfocus="entry3"
            self.entry4.configure(state="disabled")
        elif self.hasfocus == "entry5":
            self.entry4.configure(state="normal")
            self.entry4.delete(0, 'end')
            self.entry4.configure(state="normal")
            self.entry4.focus()
            self.hasfocus="entry4"
            self.entry5.configure(state="disabled")
        else:
            self.entry5.configure(state="normal")
            self.entry5.delete(0, 'end')
            self.entry5.configure(state="normal")
            self.entry5.focus()
            self.hasfocus="entry5"

    def do_enter(self):
        if len(self.name1.get() + self.name2.get() + self.name3.get() + self.name4.get() + self.name5.get()) == 5:
            newRow()
        else:
            self.FiveLetterWord = Label(self, height=2, width=22, bg='#2d2d2f', fg='white', text='Word must be of 5 letters', font="Calibri 20", background=self.windowBG,highlightbackground='#3A3A3C', highlightthickness=2)
            self.FiveLetterWord.place(x=100, y=100)
            self.update_idletasks()
            self.after(3000)
            self.FiveLetterWord.destroy()

    def createEntry(self):
        
        self.name1 = StringVar()
        self.entry1 = ttk.Entry(self, width=2, textvariable = self.name1, font="Calibri 15", justify='center')
        self.entry1.place(x=90, y=40+self.d)

        self.name2 = StringVar()
        self.entry2 = ttk.Entry(self, width=2, textvariable = self.name2, font="Calibri 15", justify='center')
        self.entry2.place(x=170, y=40+self.d)
        self.entry2.configure(state="disabled")

        self.name3 = StringVar()
        self.entry3 = ttk.Entry(self, width=2, textvariable = self.name3, font="Calibri 15", justify='center')
        self.entry3.place(x=250, y=40+self.d)
        self.entry3.configure(state="disabled")

        self.name4 = StringVar()
        self.entry4 = ttk.Entry(self, width=2, textvariable = self.name4, font="Calibri 15", justify='center')
        self.entry4.place(x=330, y=40+self.d)
        self.entry4.configure(state="disabled")

        self.name5 = StringVar()
        self.entry5 = ttk.Entry(self, width=2, textvariable = self.name5, font="Calibri 15", justify='center')
        self.entry5.place(x=410, y=40+self.d)
        self.entry5.configure(state="disabled")

        self.entry1.focus() ; hasfocus = "entry1"
        self.name1.trace_add("write", callback1)
        self.name2.trace_add("write", callback2)
        self.name3.trace_add("write", callback3)
        self.name4.trace_add("write", callback4)
        self.name5.trace_add("write", callback5)


        
root=root()
root.mainloop()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-05 19:03:43

您可以使用winfo_children获取根窗口的所有子部件的列表,然后可以迭代该列表以销毁每个子部件。

代码语言:javascript
复制
for child in root.winfo_children():
    child.destroy()

从那时起,您就有一个根窗口,其中没有任何内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72132162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档