简体   繁体   English

您如何在 Tkinter 中设置条目参数“显示”以始终在条目字段中显示固定数量的星号?

[英]How do you set Entry parameter 'show' in Tkinter to always show fixed number of asterisks in Entry field?

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.我将“show”参数设置为“*”,这样 Entry 字段就不会显示输入的内容。我遇到的问题是,它暗示了这个词的实际长度。 So, for example, if I type in word 'blue', it will show as '****' which hints it's a 4 characters long word.因此,例如,如果我输入单词“blue”,它将显示为“****”,这表明它是一个 4 个字符长的单词。 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()

Use this code:使用此代码:

from tkinter import *
root = Tk()

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

root.mainloop()

Make sure to copy the code in the show = carefully.确保仔细复制show =的代码。 It will be not the same length as an empty space so to throw people off.它的长度与空旷的空间不同,因此可以将人们赶走。 Copy it here (between the quotes): " ‍ " Note that it is an iEmpty blank character复制到这里(在引号之间):" ‍ " 注意它是一个iEmpty空白字符

If you want, you can set the show to this:如果需要,您可以将节目设置为: (just a space key) (只是一个空格键)

How do you set Entry parameter 'show' in Tkinter to always show fixed number of asterisks in Entry field?您如何在 Tkinter 中设置条目参数“显示”以始终在条目字段中显示固定数量的星号? .. 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.没有简单的方法,除了将 show 设置为空白之外,没有办法使用show属性来隐藏长度。

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.如果您需要更安全的东西,您必须创建自己的绑定来接受来自用户的输入,然后显示其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM