노는게 제일 좋습니다.

Tkinter Entry widget control text data 본문

Python/기타 공부

Tkinter Entry widget control text data

노는게 제일 좋습니다. 2016. 9. 4. 19:43

reference http://effbot.org/tkinterbook/entry.htm#Tkinter.Entry.insert-method


Entry has index. Index specify position. You can use index to access data in Entry.

Example) ABCDE

index 2 means hear.(between A and B)          AB CDE


In webpage http://effbot.org/tkinterbook/entry.htm#Tkinter.Entry.insert-method , index is being said.

 

Indexes

The Entry widget allows you to specify character positions in a number of ways:

  • Numerical indexes
  • ANCHOR
  • END
  • INSERT
  • Mouse coordinates (“@x”)

Numerical indexes work just like Python list indexes. The characters in the string are numbered from 0 and upwards. You specify ranges just like you slice lists in Python: for example, (0, 5) corresponds to the first five characters in the entry widget.

ANCHOR (or the string “anchor”) corresponds to the start of the selection, if any. You can use the select_from method to change this from the program.

END (or “end”) corresponds to the position just after the last character in the entry widget. The range (0, END) corresponds to all characters in the widget.

INSERT (or “insert”) corresponds to the current position of the text cursor. You can use the icursor method to change this from the program.

Finally, you can use the mouse position for the index, using the following syntax:

    "@%d" % x

where x is given in pixels relative to the left edge of the entry widget.


Look at the following code.

1. Click 'printMsg' : print ent1 and ent2

2. Click 'insert' : insert ent2 into middle of end1

from tkinter import *
root = Tk()

#Define function
def c_print():
    temp = ent1.get()
    dust = ent2.get()
    messagebox.showinfo("printWindow","You typed\n"+temp+"\n"+dust)

def c_insert():
    temp = ent1.index(END)
    ent1.insert(int(temp/2),ent2.get())

#Define Object and place them.
lbl1 = Label(root,text="First").grid()
lbl2 = Label(root,text="Second").grid()

ent1 = Entry(root)
ent1.grid(row=0,column=1,columnspan=2)
ent2 = Entry(root)
ent2.grid(row=1,column=1,columnspan=2)

btn1 = Button(root, text="PrintMsg",width=9,command=c_print).grid(row=2,column=1)
btn2 = Button(root, text="Insert", width=9,command=c_insert).grid(row=2,column=2)


index(n) returns numeric value of n 

** n is index.

'Python > 기타 공부' 카테고리의 다른 글

tkinter modify widget option  (0) 2016.09.17
python random  (0) 2016.09.17
Tkinter tutorial(link)  (0) 2016.09.04
Tkinter PhotoImage resize  (1) 2016.09.04
Tkinter How to use grid for PhotoImage  (0) 2016.09.04
Comments