当用户单击文本框时如何删除文本框内的文本?

问题描述 投票:0回答:1

我使用 Tkinter 模块在 Python 中创建了这个 GUI 程序来学习如何执行此操作:

(https://i.stack.imgur.com/Ey76v.png)

但是,如果“输入用户名”文本在您单击它后消失,就像您输入身份验证信息时它在盗版每个网站上的工作方式一样,我希望它

我该如何使用这段代码来做到这一点?:

from tkinter import * 


count = 0

def click(): 
    global count 
    count += 1
    print("You have clicked the button "+str(count)+" Times")

def submit():
    Username = Enter_Box.get() 
    print("You have set your username to: "+Username)
    delete()

def delete(): 
    Enter_Box.delete(0,END)

def backspace(): 
    Enter_Box.delete(len(Enter_Box.get())-1,END)

Window = Tk() 

Photo = PhotoImage(file="Pig2.png")

Like_Button = PhotoImage(file="Like-Button2.png")

Window.geometry("900x600")

Window.title("The Ultimate GUI Program")

Window.iconbitmap("AngryBird9.ico")


button = Button(Window,
                text="Click me",
                command=click,
                font=("Comic Sans,",10,"bold"),
                fg="Red",
                bg="black",
                activeforeground="Red",
                activebackground="black",
                image=Like_Button,
                compound="bottom")


button.place(x=50,y=50) 

Window.config(background="white") 

 
Label = Label(Window, 
              text="You are using the best program ever!", 
              font=("Arial",10,"bold"),
              fg="red",
              bg="white",relief=RAISED,bd=10,
              padx=10,
              pady=10,image=Photo,
              compound="bottom")


Label.place(x=170,y=170)  

Enter_Box = Entry(Window,font=("Arial"),fg="Black",bg="Gray") 
Enter_Box.place(x=460,y=220)

Submit_button = Button(Window,text="Submit",command=submit)
Submit_button.place(x=700,y=220)

Delete_button = Button(Window,text="Delete",command=delete)
Delete_button.place(x=750,y=220)

BackSpace_button = Button(Window,text="Backspace",command=backspace)
BackSpace_button.place(x=795,y=220)

Enter_Box.insert(0,"Enter username")

Window.mainloop()

我试图搜索这个问题,但是我所能找到的只是如何在用户输入字符时删除文本,而不仅仅是在单击时删除文本。

python user-interface input click
1个回答
0
投票

您想要做的事情称为“占位符” - 请参阅这个问题的答案,了解如何做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.