I set the 'show' parameter to '*' so that Entry field doesn't reveal what was typed in. The issue I am having is, it hints how long the word actually is. So, for example, if I type in word 'blue', it will show as '****' which hints it's a 4 characters long word. Also, the solution code needs to work with the random function filling in the above mentioned field
How do I solve this easy way?
EDIT: To put things into a context this is the code of the app I made.
import random
from tkinter import *
def num_guess():
try:
int(num_input.get())
except ValueError:
Ans = "You must enter a number in your number field!\n"
result_field.insert(INSERT, Ans)
if int(num_input.get()) == int(hidden_num):
Ans = "Congrats you guessed correctly!\n"
result_field.insert(INSERT, Ans)
secret_num.delete(0, END)
secret_num.insert(END, hidden_num)
elif int(num_input.get()) in range(int(hidden_num) - 2, int(hidden_num) + 3):
Ans = "You are very close to secret number!..." + str(num_input.get()) + "\n"
result_field.insert(INSERT, Ans)
elif int(num_input.get()) in range(int(hidden_num) - 5, int(hidden_num) + 6):
Ans = "You are close to secret number!..." + str(num_input.get()) + "\n"
result_field.insert(INSERT, Ans)
elif int(num_input.get()) in range(int(hidden_num) - 10, int(hidden_num) + 11):
Ans = "You are relatively close to secret number!..." + str(num_input.get()) + "\n"
result_field.insert(INSERT, Ans)
elif int(num_input.get()) in range(int(hidden_num) - 15, int(hidden_num) + 16):
Ans = "You are far from secret number!..." + str(num_input.get()) + "\n"
result_field.insert(INSERT, Ans)
else:
Ans = "You are very far from secret number!..." + str(num_input.get()) + "\n"
result_field.insert(INSERT, Ans)
def start_new():
global hidden_num
result_field.delete('1.0', END)
secret_num.delete(0, END)
secret_num.insert(END, random.randint(1,100))
hidden_num = secret_num.get()
secret_num.delete(0, END)
secret_num.insert(END, "***")
result_field.insert(INSERT, "Now enter your number as a guess!\n")
def hide_num():
global hidden_num
try:
int(secret_num.get())
hidden_num = secret_num.get()
secret_num.delete(0, END)
secret_num.insert(END, "***")
result_field.insert(INSERT, "Now enter your number as a guess!\n")
except ValueError:
Ans = "You must enter a number in secret number field!\n"
result_field.insert(INSERT, Ans)
hidden_num = 0
window = Tk()
window.title('Guess the Number!')
Label(window, text = "").grid(row=0)
Label(window, text = "Secret number:").grid(row=7, column=1, padx=8)
Label(window, text = "Enter your number:").grid(row=8, column=1, padx=8)
Label(window, text = "").grid(row=10)
#find textbox code!
result_field = Text(window, width=54, height=18)
result_field.grid(row=1, column=1, columnspan=6, rowspan=6, padx=8)
result_field.insert(INSERT, "Click 'Start new' to start a new game!\n")
sb1 = Scrollbar(window)
sb1.grid(row=1, column=7, rowspan=6, sticky = 'NS')
result_field.configure(yscrollcommand=sb1.set)
sb1.configure(command = result_field.yview)
secret_num = Entry(window, width=6)
secret_num.grid(row=7, column=2)
num_input = Entry(window, width=6)
num_input.grid(row=8, column=2)
b1 = Button(window, text = "Take a guess!", command=num_guess, width=12)
b1.grid(row=9, column=1, pady=8, padx=8)
b2 = Button(window, text = "Start new", command=start_new, width=12)
b2.grid(row=9, column=2, pady=8, padx=8)
b3 = Button(window, text = "Quit", command=window.destroy, width=12)
b3.grid(row=9, column=3, pady=8, padx=8)
b4 = Button(window, text = "Hide number", command=hide_num, width=12)
b4.grid(row=7, column=3, sticky = 'W')
window.mainloop()
How do you set Entry parameter 'show' in Tkinter to always show fixed number of asterisks in Entry field? .. How do I solve this easy way?
There is no easy way, and there's no way to use the show
attribute to hide the length other than to set show to whitespace.
If you need something more secure you'll have to create your own bindings that accept input from a user and then display something else.