如何在Tkinter中将Entry参数'show'设置为始终在Entry字段中显示固定数量的星号?

时间:2019-05-31 19:26:11

标签: python tkinter passwords tkinter-entry

我将'show'参数设置为'*',以便Entry字段不显示键入的内容。我遇到的问题是,它提示单词实际上有多长时间。因此,例如,如果我输入单词“ blue”,它将显示为“ ****”,这暗示它是一个4个字符长的单词。另外,解决方案代码需要使用填充上述字段中的随机函数

如何解决这种简单的方法?

编辑:要将内容放到上下文中,这是我制作的应用程序的代码。

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()

2 个答案:

答案 0 :(得分:0)

使用此代码:

from tkinter import *
root = Tk()

passwordInput = Entry(root, show = "   ‍   ")
passwordInput.pack()

root.mainloop()

请确保仔细复制show =中的代码。它的长度与空白区域的长度是不一样的,以便将人扔掉。将其复制到此处(在引号之间):“ ‍” 请注意,它是一个iEmpty空白字符

如果需要,可以将显示设置为此:(只是一个空格键)

答案 1 :(得分:0)

  

如何在Tkinter中将Entry参数'show'设置为始终在Entry字段中显示固定数量的星号? ..如何解决这种简单的方法?

没有简单的方法,除了将show设置为空白之外,没有其他方法可以使用show属性隐藏长度。

如果您需要更安全的东西,则必须创建自己的绑定以接受用户输入,然后显示其他内容。