So as the title gives away, I wanna replace the letters of a word (That you input) to underscore ( _ ) or atleast to something different.
Now I have made a topic about this before and got some help, but I couldn’t figure out how to apply it to my code. So with his help and we both agreed it would be best if I made a new topic and also translated my code (Originally I am Dutch so) for better clarity.
So in the section “class ChooseMain(tk.Frame):” I have a user input a word that will be the word to guess.
The code in question for the input is:
self.woordEntry = tk.Entry(self, textvariable=self.controller.shared_data["chosenWord"])
Which in turn is sent to the next class called “ChosenMain” through this code:
btnContinue = tk.Button(self, text="Continue", command=lambda: controller.show_frame("ChosenMain"))
Now in the next class (“ChosenMain” in this case) it will show 3 things. 1 is word that was inputted on the previous screen and 2 buttons.
The word that was put in is displayed like this:
wordLabel = tk.Label(self, textvariable=self.controller.shared_data["chosenWord"], font=("futura"))
wordLabel.pack(side="top")
Now this shows the word that was input as normal but obviously I don’t want to show it as normal, preferably as _ _ _ _ or something similar. And that is where I am stuck.
Now as mentioned I did get help before this which was here: Trying to replace label text to underscore for a game of hangman
As mentioned there, I tried the solutions posted there but I just couldn’t get the wordLabel section to be changed to _ or * or anything really.
I’ll post my full code below this, and I hope I didn’t forget to translate something. Again if there is more information required or whatever. Feel free to comment here or message me and I will try my best to help!
#Hangman
import tkinter as tk
class Hangman(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.shared_data = {
"chosenWord": tk.StringVar()
}
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, minsize=500, weight=1)
container.grid_columnconfigure(0, minsize=600, weight=1)
self.frames = {}
for F in (ChooseMain, ChosenMain):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("ChooseMain")
def show_frame(self, page_name):
for frame in self.frames.values():
frame.grid_remove()
frame = self.frames[page_name]
frame.grid()
class ChooseMain(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Choose your word")
label.pack(side="top", fill="x", pady="10")
self.wordEntry = tk.Entry(self, textvariable=self.controller.shared_data["chosenWord"])
self.wordEntry.pack(pady=10)
btnContinue = tk.Button(self, text="Continue", command=lambda: controller.show_frame("ChosenMain"))
btnContinue.pack()
class ChosenMain(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Guess the word!")
label.pack(side="top", fill="x", pady=10)
wordLabel = tk.Label(self, textvariable=self.controller.shared_data["chosenWord"], font=("futura"))
wordLabel.pack(side="top")
self.wentry = tk.Entry(self)
self.wentry.pack(pady=10)
GuessButton = tk.Button(self, text="Guess!", command=self.GuessWord)
GuessButton.pack()
NewWoord = tk.Button(self, text="Choose different word", command=self.ChangeWord)
NewWoord.pack()
def GuessWord(self):
return True
def ChangeWord(self):
self.refresh()
self.controller.show_frame("ChooseMain")
def refresh(self):
self.wentry.delete(0, "end")
#self.text.delete("1.0", "end")
self.wentry.focus_set()
if __name__ == "__main__":
galgbord = Hangman()
galgbord.mainloop()