我目前正在Linux中编写一个小GUI,用于教我俱乐部的年轻成员如何进行版本控制。我正在使用python Tkinter lib。
我创建了一个文本空间来插入文本,并以下列方式放置了示例文本:
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)
我想知道是否有任何办法让光标放在盒子上时文字消失了?
答案 0 :(得分:1)
我认为这可能会回答您的问题,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()
希望有所帮助。