为什么将钥匙绑定到入口小部件不起作用?
大家好,我正在尝试创建一个文本空间,该文本空间像常规密码一样替代字母或任何输入中的任何输入。但是,为什么如果我按下任何键,则第一个输入为空,为什么我将任何键绑定到函数“文本”呢?因为在函数i打印(e.get())中,如果我键入“ M”,则输出在控制台中为空?为什么它忽略输入以及如何简单地显示文本区域中的星号并将正常密码绑定到我的代码中的变量“ A”上?使用e.delete和e.insert,我只是切下了最后一个字符,然后用星号替换它是我的代码
from tkinter import *
a = ""
window = Tk()
def entra():
global a
if a == "cgf":
print ("a")
else:
print("noo")
def text (event):
global a
a = a + e.get()
print(e.get())
e.delete(len(e.get()) - 1)
e.insert(END, "*")
e = Entry(window, borderwidth=3, width=50, bg="white", fg="black")
e.place(x=100, y=35)
e.bind("<Key>", text)
bottone_entra = Button (window, text = "Entra", borderwidth=2.5,command = entra)
bottone_entra.place (x=100, y=65)
etichetta = Label(window, text = "PASSWORD", bg = "#2B2B2B", fg = "white")
etichetta.place(x=97.5, y=10)
window.geometry ("500x500")
window.title ("Password")
window.configure(bg = "#2B2B2B")
window.mainloop()
HI guys i'm trying to simply create a text space that replaces the letters or any input into asterisks just like happen with the regular passwords. But why if i bind any key to the function "text" if i press any key the first input is empty? Becuase if i type "m" when in the function i print(e.get()) the output is empty in the console? Why it ignores the input and how to simply show the asterisk in the text area and bind the normal password to the variable "a" just like in my code? With e.delete and e.insert i simply cut the last character and replace it with asterisk here is my code
from tkinter import *
a = ""
window = Tk()
def entra():
global a
if a == "cgf":
print ("a")
else:
print("noo")
def text (event):
global a
a = a + e.get()
print(e.get())
e.delete(len(e.get()) - 1)
e.insert(END, "*")
e = Entry(window, borderwidth=3, width=50, bg="white", fg="black")
e.place(x=100, y=35)
e.bind("<Key>", text)
bottone_entra = Button (window, text = "Entra", borderwidth=2.5,command = entra)
bottone_entra.place (x=100, y=65)
etichetta = Label(window, text = "PASSWORD", bg = "#2B2B2B", fg = "white")
etichetta.place(x=97.5, y=10)
window.geometry ("500x500")
window.title ("Password")
window.configure(bg = "#2B2B2B")
window.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需手动为其构建逻辑,
tkinter
幸运的是已经提供了它。您只需要为条目小部件设置一个额外的选项,show ='*'
:为什么要避免构建/使用自己的逻辑,一开始就应该避免构建/使用此逻辑您要做
get()
,您只会获得****,而不是最初的文字。You don't have to manually build a logic for it,
tkinter
luckily already provides it. You just have to set an extra option to the entry widget,show='*'
:There are many reasons as to why you should avoid building/using this logic of your own, to begin with, once you do
get()
, you will only get **** and not whatever the text originally was.