默认文本以及列表文本变量Entry小部件Tkinter [英] Default text as well as list textvariable Entry widget Tkinter

查看:138
本文介绍了默认文本以及列表文本变量Entry小部件Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输入框,由于获取值,我在其中输入了textvariable的列表选项.

I have the following Entry box where due to obtaining values I have put a list option in for textvariable.

但是我想知道是否可以在背景中放置默认文本,以便显示每个框中需要哪些值(例如灰度文本,值1",值2"等.).

However I was wondering if it would be possible to put a default text in the background so to show which values are required in each box (like a greyscale text, 'Value 1, value 2 etc..).

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

在框内单击鼠标后,是否可以添加可能会更改"textvariable"的内容,还是可以仅添加另一个textvariable或文本以设置默认文本?

Can I add in maybe something change 'textvariable' upon a mouse click inside the box or can I simply just add in another textvariable or text to set a default text?

  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小部件中放入默认文本,可以使用insert()方法,如

In order to put default text in your Entry widget, you can use the insert() method as described here.

box.insert(0, "Value 1")    # Set default text at cursor position 0.

现在,为了在用户在框内单击鼠标后更改box的内容,您需要将事件 bind 绑定到Entry对象.例如,下面的代码在单击该框时将其删除. (您可以在此处中了解事件和绑定.)完整的例子.

Now in order to change the contents of box after the user performs a mouse click inside the box, you will need to bind an event to the Entry object. For example, the following code deletes the contents of the box when it is clicked. (You can read about event and bindings here.) Below I show a full example of this.

请注意,删除框中的文本可能仅对第一次单击(即删除默认内容时)才是实际的,因此我创建了一个全局标志clicked来跟踪是否已单击它.

Note that deleting the text in the box is probably only practical for the first click (i.e. when deleting the default contents), so I created a global flag clicked to keep track of whether it has been clicked.

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

这篇关于默认文本以及列表文本变量Entry小部件Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆