简体   繁体   中英

understanding the insert method in Tkinter's Entry widget

I've been going through a tutorial on Python's Tkinter, I'm currently working with the Entry widget and just came across the insert method. I understand what the method does (makes a default string to appear in a text entry field), but what I don't get is the method's first parameter, which to the best of my understanding is the index at which the string should start. now here comes the confusion. When playing around with it, giving different values for the index parameter, every time I run it the text appears in the same spot (at the very beginning). So as best I can tell, I'm either doing something wrong or, I'm misunderstanding the documentation. this is a snippet from my code:

 e1 = Entry(master)
 e1.insert(0,"First Name")

When ever I run this, weather the index is 0, 10 or 100 the text "First Name" always appears at the very beginning of the text field

First, the statement "makes a default string appear" is not quite true. While it can be used to insert default text, it more correctly is described simply as inserting text, period. It can be default text, replacement text, additional text, whatever you want.

Any index that is before the first character is treated as 0 (zero). Any index that is after the last character is treated as being the end. When you insert something into an entry widget that is empty, every index is treated as 0. Thus, the index is mostly useful for inserting text somewhere into existing text.

For example:

e1 = Entry(master)
e1.insert(0, "hello")
e1.insert("end", "world")
e1.insert(5, ", ")

As an application programmer, you will almost never use the insert method with anything other than an index of 0 (zero) or "end", and perhaps "insert". However, when you try to add advanced functionality (eg: spell checking, auto-complete, etc) you will find the index to be highly useful.

In this line:

text.insert('1.0', 'here is my\ntext to insert')

The "1.0" here is the position where to insert the text, and can be read as "line 1, character 0". This refers to the first character of the first line. Historically, especially on Unix, programmers tend to think about line numbers as 1-based and character positions as 0-based.

The text to insert is just a string. Because the widget can hold multi-line text, the string we supply can be multi-line as well. To do this, simply embed \n (newline) characters in the string at the appropriate locations.

From the Tk docs https://tkdocs.com/tutorial/text.html

you use it when you use multiple insert() method

for example:

e = Entery(window)

e.pack()

e.insert(0, "Hi")

e.insert(2, " there")

Note : in " there" you should have a space behind it or else it will appear like this (Hithere)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM