I am recreating the game Wordle for practice.
I am trying to find a function that allows me to destroy all widgets in my tkinter window.
root.destroy()
isn't a solution because it just closes the whole window.
I want to use this function to, whenever we win the game, restart the whole program without having to manually do it.
Program:
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()
CodePudding user response:
You can use winfo_children
to get a list of all child widgets of the root window, and then you can iterate over the list to destroy each child.
for child in root.winfo_children():
child.destroy()
From that point on, you have a root window with nothing in it.