Python Tkinter 的 GUI 提示中的文本消失

Disappearing text in GUI prompt for Python Tkinter

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

我制作了一个文本 space 来插入文本,并按以下方式在其中放置了示例文本:

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()

希望对您有所帮助。