标签:list tkinter ttk python user-interface
我有以下输入框,由于获取值,我在其中输入了textvariable的列表选项.
但是我想知道是否可以在背景中放置默认文本,以显示每个框中需要的值(例如灰度文本,“值1”,“值2”等.).
self.numbers = [StringVar() for i in xrange(self.number_boxes) ] #Name available in global scope.
box=Entry(self.frame_table,bg='white',borderwidth=0, width=10, justify="center", textvariable=self.numbers[i])
我是否可以在框内单击鼠标后添加一些更改“文本变量”的内容,还是可以仅添加另一个文本变量或文本来设置默认文本?
self.box = []
for i in xrange(self.number_boxes):
self.clicked = False
self.box.append(Entry(self.frame_table,bg='white',borderwidth=0, width=10, justify="center", textvariable=self.numbers[i], fg='grey'))
self.box[i].grid(row=row_list,column=column+i, sticky='nsew', padx=1, pady=1)
self.box[i].insert(0, "Value %g" % float(i+1))
self.box[i].bind("<Button-1>", self.callback)
解决方法:
为了将默认文本放在Entry小部件中,可以使用here所述的insert()方法.
box.insert(0, "Value 1") # Set default text at cursor position 0.
现在,为了在用户在框内单击鼠标后更改框的内容,您需要将事件绑定到Entry对象.例如,以下代码在单击该框时将其删除. (您可以阅读有关事件和绑定here的信息.)在下面,我显示了一个完整的示例.
请注意,删除框中的文本可能仅对第一次单击有效(即删除默认内容时),因此我创建了一个单击的全局标志以跟踪是否已单击它.
from tkinter import Tk, Entry, END # Python3. For Python2.x, import Tkinter.
# Use this as a flag to indicate if the box was clicked.
global clicked
clicked = False
# Delete the contents of the Entry widget. Use the flag
# so that this only happens the first time.
def callback(event):
global clicked
if (clicked == False):
box[0].delete(0, END) #
box[0].config(fg = "black") # Change the colour of the text here.
clicked = True
root = Tk()
box = [] # Declare a list for the Entry widgets.
box.append(Entry(fg = "gray")) # Create an Entry box with gray text.
box[0].bind("<Button-1>", callback) # Bind a mouse-click to the callback function.
box[0].insert(0, "Value 1") # Set default text at cursor position 0.
box.append(Entry(fg = "gray")) # Make a 2nd Entry; store a reference to it in box.
box[1].insert(0, "Value 2")
box[0].pack() #
box[1].pack()
if __name__ == "__main__":
root.mainloop()
标签:list,tkinter,ttk,python,user-interface
来源: https://codeday.me/bug/20191101/1981309.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。