Python學習記錄--關於Tkinter Entry(文本框)的選項、方法說明,以及一些示例。
屬性(Options)
- background(bg)
- borderwidth(bd)
- cursor
- exportselection
- font
- foreground(fg)
- highlightbackground
- highlightcolor
- highlightthickness
- insertbackground
- insertborderwidth
- insertofftime
- insertontime
- insertwidth
- justify
- relief
- selectbackground
- selectborderwidth
- selectforeground
- show
- state
- takefocus
- textvariable
- width
- xscrollcommand
background(bg)
- Type: color
- 說明:文本框的背景顏色
#示例 from Tkinter import * top = Tk() text = Entry(top, background = 'red') text.pack() mainloop()
borderwidth(bd)
- Type: distance
- 說明:文本框邊框寬度
#示例 text = Entry(top, borderwidth = 3)
cursor
- Type: cursor
- 待定
exportselection
- Type: flag
- 待定
font
- Type: font
- 說明:文字字體。值是一個元祖,font = ('字體','字號','粗細')
#示例 text = Entry(top, font = ('Helvetica', '14', 'bold')
foreground
- Type: color
- 說明:文字顏色。值為顏色或為顏色代碼,如:'red','#ff0000'
#示例 text = Entry(top, foreground = 'red') #正確 text = Entry(top, foreground = '#ff0000') #正確 text = Entry(top, foreground = 'ff0000') #錯誤,必須加上#號
highlightbackground
- Type: color
- 說明:文本框高亮邊框顏色,當文本框未獲取焦點時顯示
- 條件:highlightthickness設置有值
#示例 text = Entry(top, highlightbackground = 'red', hightlightthickness = 1)
highlightcolor
- Type: color
- 說明:文本框高亮邊框顏色,當文本框獲取焦點時顯示
- 條件:highlightthickness設置有值
#示例 text = Entry(top, highlightcolor = 'red', hightlightthickness = 1)
highlightthickness
- Type: distance
- 說明:文本框高亮邊框寬度。(官網上說有默認值1或2,但如果不設置,實際上沒有值,可能和操作系統有關系)
#示例 text = Entry(top, highlightcolor = 'red', hightlightthickness = 1)
insertbackground
- Type: color
- 說明:文本框光標的顏色
#示例 text = Entry(top, insertbackground = 'red')
insertborderwidth
- Type: distance
- 說明:文本框光標的寬度。(有問題,官網未有說明,待定)
#示例 text = Entry(top, insertborderwidth = 3)
insertofftime
- Type: int
- 說明:文本框光標閃爍時,消失持續時間,單位:毫秒
#示例 text = Entry(top, insertofftime = 50)
insertontime
- Type: int
- 說明:文本框光標閃爍時,顯示持續時間,單位:毫秒
#示例 text = Entry(top, insertontime = 50)
insertwidth
- Type: int
- 說明:文本框光標寬度
#示例 text = Entry(top, insertwidth = 3)
justify
- Type: const
- 待定
relief
- Type: const
- 說明:文本框風格,如凹陷、凸起,值有:flat/sunken/raised/groove/ridge
#示例 text = Entry(top, relief = 'sunken')
selectbackground
- Type: color
- 說明:選中文字的背景顏色
#示例 text = Entry(top, selectbackground = 'red') text = Entry(top, selectbackground = '#ff0000')
selectborderwidth
- Type: int
- 說明:選中文字的背景邊框寬度
#示例 text = Entry(top, selectborderwidth = 3)
selectforeground
- Type: color
- 說明:選中文字的顏色
#示例 text = Entry(top, selectforeground = 'red') text = Entry(top, selectforeground = '#ff0000')
show
- Type: character
- 說明:指定文本框內容顯示為字符,值隨意,滿足字符即可。如密碼可以將值設為*
#示例 text = Entry(top, show = '*')
state
- Type: const
- 說明:文框狀態,分為只讀和可寫,值為:normal/disabled
#示例 text = Entry(top, state = 'normal') #可操作 text = Entry(top, state = 'disabled') #不可操作
takefocus
- Type: flag
- 說明:是否能用TAB鍵來獲取焦點,默認是可以獲得
#示例 待定
textvariable
- Type: variable
- 說明:文本框的值,是一個StringVar()對象
#示例 default_value = StringVar() default_value.set('This is a default value') text = Entry(top, textvariable = default_value)
width
- Type: int
- 說明:文本框寬度
#示例 text = Entry(top, width = 50)
xscrollcommand
- Type: callback
- 說明:回調函數
#示例 def callback(): #code text = Entry(top, command = callback)
方法(Methods)
- insert
- delete
- icursor
- get
- index
- selection_adjust, select_adjust
- selection_clear, select_clear
- selection_from, select_from
- selection_present, select_present
- selection_range, select_range
- selection_to, select_to
- scan_mark
- scan_dragto
- xview
- xview_moveto, xview_scroll
insert(index, text)
向文本框中插入值,index:插入位置,text:插入值
#示例 text.insert(0, '內容一') #在文本框開始位置插入“內容一” text.insert(10, '內容二') #在文本框第10個索引位置插入“內容二” text.insert(END, '內容三') #在文本框末尾插入“內容三”
delete(index), delete(from, to)
刪除文本框里直接位置值
#示例 text.delete(10) #刪除索引值為10的值 text.delete(10, 20) #刪除索引值從10到20之前的值 text.insert(0, END) #刪除所有值
icursor(index)
將光標移動到指定索引位置,只有當文框獲取焦點后成立
#示例 text.icursor(10) #移動光標到索引為10的位置
get()
獲取文件框的值
#示例 text.get() #返回文本框的值
index(index)
返回指定的索引值
#示例 text.index(2)
selection_adjust(index), select_adjust(index)
選中指定索引和光標所在位置之前的值
#示例 text.selection_adjust(2) #選中索引為2和光標所有位置之前的所有值
selection_clear(), select_clear()
清空文本框
#示例 text.selection_clear()
selection_from(index), select_from(index)
待定
selection_range(start, end), select_range(start, end)
選中指定索引之前的值,start必須比end小
#示例 text.selection_range(2, 10) #選中索引為2和10之前的所有值
selection_to(index), select_to(index)
選中指定索引與光標之間的值(感覺和selection_adjust差不多)
#示例 text.selection_to(2) #選中索引為2和所光標所在位置之前的值
scan_mark(x)
待定
scan_dragto(x)
待定
xview(x)
待定
原文:http://blog.fuweiyi.com/Python/2013/03/04/a-python-Tkinter-Entry.html