为什么将钥匙绑定到入口小部件不起作用?

发布于 2025-01-24 16:24:35 字数 960 浏览 2 评论 0原文

大家好,我正在尝试创建一个文本空间,该文本空间像常规密码一样替代字母或任何输入中的任何输入。但是,为什么如果我按下任何键,则第一个输入为空,为什么我将任何键绑定到函数“文本”呢?因为在函数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意犹 2025-01-31 16:24:35

您无需手动为其构建逻辑,tkinter幸运的是已经提供了它。您只需要为条目小部件设置一个额外的选项,show ='*'

e = Entry(window, borderwidth=3, width=50, bg="white", fg="black", 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='*':

e = Entry(window, borderwidth=3, width=50, bg="white", fg="black", 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文