Skip to main content
 首页 » 编程设计

python之在 Python Tkinter 的 GUI 提示符中消失的文本

2025年05月04日25小虾米

我目前正在 Linux 中编写一个小型 GUI,用于教我俱乐部的年轻成员如何进行版本控制。我正在使用 python Tkinter 库。

我创建了一个文本空间来插入文本,并按以下方式在其中放置了示例文本:

self.directory = tk.Text(self,height = 0)       
self.directory.grid(row = 0, column = 1, pady = 10, padx = 2)     
self.directory = self.directory.insert(tk.END,"ex. /home/your_username/desktop")     
self.dirLabel = tk.Label(self,text = "Directory Path:")     
self.dirLabel.grid(row = 0, column = 0)     

我想知道是否有任何方法可以让当光标放在框上时文本消失?

请您参考如下方法:

我认为这可以回答您的问题,How do I prepopulate a text field with suggested text in Tkinter?

相关摘录:

import Tkinter as tk 
 
tk.Tk() 
 
textbox = tk.Text(height=10, width=10) 
textbox.insert(tk.END, "Default") 
textbox.pack() 
 
# This is for demonstration purposes 
tk.Text(height=10, width=10).pack() 
 
def default(event): 
    current = textbox.get("1.0", tk.END) 
    if current == "Default\n": 
        textbox.delete("1.0", tk.END) 
    elif current == "\n": 
        textbox.insert("1.0", "Default") 
 
textbox.bind("<FocusIn>", default) 
textbox.bind("<FocusOut>", default) 
 
tk.mainloop() 

希望对您有所帮助。