程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Tkinter - Chapter 6.2 method of input control

編輯:Python

6.2 Enter the method of the control

6.2.1 Method

Method describe delete(first, last=None) Delete the selected text . The beginning is first,last If there is no definition , Only delete first Characters in position .get() Get the current content of the input control . The return value is a string icursor(index) Move the insertion cursor to the specified position .index(index) return index value . Is an integer number .insert(index, string) stay index Insert character at position .insert(INSERT,string) Insert a character at the position of the insertion cursor insert(END,string) Add text selection_adjust(index)
select_adjust(index) Adjust selection to index Location . If index Has been chosen , No action .selection_clear()
select_clear() Clear the current selection selection_from(index)
select_from(index) Set the starting position of the selection from index Start , Need and select_to() In combination with .selection_present()
select_present() Check if any characters are selected .selection_range(start, end)
select_range(start, end) Select from start To end The text of .selection_to(index)
select_to(index) Select from the cursor to index The text of the location . If used select_from(), In the use of select_from() Defined index.xview(index) Show index The text at . The length used to process the string is greater than Entry The display width of . This method guarantees index The character at will be displayed .xview_moveto(fraction) Displays the specified content . This method treats the length of the entire input as 1, The input parameter is percentage . such as 0.5 It means that the middle part of the input content is moved and displayed .xview_scroll(number, what) Horizontal scrolling number A place .what Designated units , It can be units, It can also be pages

6.2.2 delete(first,last=None)
Delete the contents of the input control . These contents are deleted by program , It is the same as the delete key .
Parameters :
first: Delete the starting position
last: Delete the end position . The default value is None. Just delete one character . It can also be END, Means to delete to the end of the line

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=20)
b1.pack()
def del_entry():
b1.delete(2,5)
b2=tk.Button(root,text='Delete Entry',command=del_entry)
b2.pack()
root.mainloop()

result :


explain :
First enter 1234567890 It's a number , Then click the button . The callback function of the button is delete(2,5). You can find 3,4,5 Been deleted . The starting position is 2( Because from 0 Start , therefore 3 Been deleted ). The end position is 5, But only delete to last-1 The location of , That's location 4( In the input control is 5).

If last Is less than or equal to first, Will not delete any characters .
6.2.3 get()
Get the current content in the input control . There are two ways to get the contents of the current input control , One is to use get, Another is to use textvariable To set the associated variable to get . Because there is no... In the input control set function , It is generally recommended to use textvariable To set and get the contents of the input control .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
txt = tk.StringVar()
b1=tk.Entry(root,width=20)
b1.pack()
b3=tk.Entry(root,width=20,textvariable=txt)
b3.pack()
def get_entry():
x = b1.get()
txt.set(x)
b2=tk.Button(root,text='Get Entry',command=get_entry)
b2.pack()
root.mainloop()

result :


explain :
First enter 12345 To the input control 1, After clicking the button , Will copy the input control 1 To the input control 2. And get the input control 1 The content of is get().
6.2.4 icursor
This method is used to move the input cursor to the specified position . This method has one parameter index, Is the position to move to . This position is from 0 The absolute position of the beginning , Not relative to the current position of the cursor .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
txt = tk.StringVar()
b1=tk.Entry(root,width=20)
b1.pack()
def move_cursor():
b1.icursor(5)
b2=tk.Button(root,text='Move Cursor',
command=move_cursor)
b2.pack()
root.mainloop()

result :


6.2.5 index
Returns the numeric type position of the input parameter . The input parameters are as follows :
tk.END : The position of the last character after
tk.INSERT : Insert the position of the cursor
tk.ANCHOR : Select the anchor location
tk.SEL_FIRST : Select where the area starts
tk.SEL_LAST : Select the position after the end of the area
‘@x’ : Input x coordinate , Returns the position of the character at that coordinate , If no character is above this coordinate , Returns the position of the character closest to this coordinate .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root)
b1.pack()
b3=tk.Label(root)
def index():
b3['text']=str(b1.index(tk.INSERT))
b2=tk.Button(root,text='Index',command=index)
b2.pack()
b3.pack()
root.mainloop()

result :

6.2.6 insert
This method inserts a string at the specified cursor . Yes 2 Parameters :
1)index: Inserts a string at the specified position .INSERT Indicates the current cursor , and END Indicates that in addition .
2)String: String to add

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=20)
b1.pack()
def insert_entry():
b1.insert(tk.END,'world!!!')
b2=tk.Button(root,text='Insert',command=insert_entry)
b2.pack()
root.mainloop()



explain :
The insertion position can be the end of the line 、 At the cursor , It can also be any designated location . As long as it is an integer . Even negative numbers don't matter .
6.2.7 selection_adjust and select_adjust
The functions of these two methods are the same . Is to expand the selected area to a given location . If the current selection area already contains the given location , There will be no change .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=20)
b1.pack()
def select_entry():
b1.select_adjust(8)
b2=tk.Button(root,text='Select Adjust',
command=select_entry)
b2.pack()
root.mainloop()

result :


explain :
If there are currently no selected characters , The method will 0 As starting point , Select to the specified location . If there are selected characters , According to the start and end positions of the selected area , Compare with the incoming location , Then decide how to adjust the selected area . Because the passed in location parameter may be smaller than the selected start location , At this time, the selected area is expanded forward .
6.2.8 selection_clear and select_clear
The purpose of these two methods is to clear the current selection .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=20)
b1.pack()
def select_entry():
b1.select_clear()
b2=tk.Button(root,text='Select Clear',
command=select_entry)
b2.pack()
root.mainloop()

result :


6.2.9 selection_from and select_from
Set the start of the selection index, The end of the index from select_to Function to determine , That is to say, the two functions need to cooperate , To complete the selected text . however select_to() It can be used alone , The starting cursor at this time can be the current cursor position , It can also be from 0 Start . See for specific usage 6.2.12 select_to Explanation .
6.2.10 selection_present and select_present
These two methods are used to detect whether any characters are selected . If any character is selected , The return value is True, It is False.

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=20)
b1.pack()
b3=tk.Label(root)
b3.pack()
def present_entry():
b3['text']=b1.selection_present()
b2=tk.Button(root,text='Select present',
command=present_entry)
b2.pack()
root.mainloop()

result :


6.2.11 selection_range and select_range
Set the selection range . There are two parameters :start and end.start Is to select the starting position , and end Is to select the end position .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=20)
b1.pack()
def range_entry():
b1.selection_range(2,6)
b2=tk.Button(root,text='Select Range',
command=range_entry)
b2.pack()
root.mainloop()

result :


6.2.12 selection_to and select_to
Select from the cursor to the given position . This given position can be in front of the cursor , It can also be behind the cursor . In fact, this function is similar to selection_from It's used in conjunction with .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=20)
b1.pack()
def to_entry():
b1.selection_from(2)
b1.selection_to(6)
b2=tk.Button(root,text='Select To',
command=to_entry)
b2.pack()
root.mainloop()

result :

6.2.13 xview
Displays the character at the given position . This function is used when the input character exceeds the given input control width , When it is necessary to quickly display or locate characters in a given position , This function is required . Especially when binding to the horizontal scroll bar .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=4)
b1.pack()
def xview_entry():
b1.xview(6)
b2=tk.Button(root,text='Xview',command=xview_entry)
b2.pack()
root.mainloop()

result :


6.2.14 xview_moveto
This method and xview similar , But the argument is a floating point number . Treat the entire length of the input as 1, Set the position to scroll to as a percentage , such as 0.5 Is to display the middle part of the whole content in the input control .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=4)
b1.pack()
def moveto_entry():
b1.xview_moveto(0.5)
b2=tk.Button(root,text='Xview Moveto',command=moveto_entry)
b2.pack()
root.mainloop()

result :


6.2.15 xview_scroll
This method has two parameters , One is number, Indicates how many positions to move . The other is what, Represents the moving unit . There are two units :unit and page.unit It is measured by the number of characters , and page It is measured by the width of the input control . in other words , If the width of the input control is 10, So the number of scrolls per time is 10 Multiple .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Entry(root,width=4)
b1.pack()
def unit_entry():
b1.xview_scroll(6,'unit')
def page_entry():
b1.xview_scroll(2,'page')
b2=tk.Button(root,text='Xview_Scroll Unit',command=unit_entry)
b2.pack()
b3=tk.Button(root,text='Xview_Scroll Page',command=page_entry)
b3.pack()
root.mainloop()

explain : Press down ’Xview_Scroll Unit’, The contents of the input box scroll forward 6 Characters ; If you press ’Xview_Scroll Page’, Then scroll forward 4 Characters , Because the width of the input field is 4.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved