close

基本用法如下:

import tkinter as tk

text = tk.Label(root, text='輸入名稱').pack(side=tk.LEFT)

content = tk.Entry(root, bd=5).pack(side=tk.LEFT)

root.mainloop()

取得輸入文字框的值:

import tkinter as tk

text = tk.Label(root, text='輸入名稱').pack(side=tk.LEFT)

var = tk.StringVar() #宣告字串變數

content = tk.Entry(root, textvariable=var, bd=5).pack(side=tk.LEFT) #把字串變數綁在Entry上

value = var.get() #取得字串的值

root.mainloop()

 

基本結構:

entry = Entry( master, option, ... )

tkinter提供Entry共18個參數如下所示:

1. bg: 輸入框背景

2. bd: 邊框的大小, 默認為2px

3. cursor: 光標的形狀設定, 如arrow, circle, cross, plus等

4. font: 文字字體

5. exportselection: 默認情況下, 如果再輸入框中選取文字片段, 默認會自動複製此段文字, 如果要忽略此功能, 必須設置exportselection=0

6. fg: 文字顏色, 值為顏色或顏色代碼, 如'red'或'#ff0000'

7. highlightcolor: 聚焦的高亮顏色

8. justify: 顯示多行文字的時候, 設置不同行之間的對齊方式, 有3種模式: LEFT, RIGHT, CENTER

9. relief: 邊框樣式, 設置組件3D效果, 有5種模式: FLAT, SUNKEN, RAISED, GROOVE, RIDGE, 默認為FLAT

10. selectbackground: 選取中文字的背景顏色

11. selectborderwidth: 選取中文字的背景邊框寬度

12. selectforeground: 選取中文字的顏色

13. show: 指定文字框內容顯示為哪種字符, 如將值設為show='*'

14. state: 文字框狀態, 分為只能讀取或可修改, 值為: NORMAL, DISABLED, 默認為state=NORMAL

15. textvariable: 文字框的值, 用來接收StringVar()的值

16. width: 文字框的寬度

17. xscrollcommand: 設置水平方向滾動條, 在使用者輸入的文字框內容寬度大於文字框顯示的寬度時使用

18. command: A procedure to be called every time the user changes the state of this checkbutton.

 

Entry提供13個呼叫函式(方法)如下所示:

1. delete( first, last=None ): Deletes characters from the widget, starting with the one at index first, up to but not including the character at position last. If the second argument is omitted, only the single character at position first is deleted.

2. get(): Returns the entry's current text as a string.

3. icursor( index ): Set the insertion cursor just before the character at the given index.

4. index ( index ): Shift the contents of the entry so that the character at the given index is the leftmost visible character. Has no effect if the text fits entirely within the entry.

5. insert ( index, s ): Inserts string s before the character at the given index.

6. select_adjust ( index ): This method is used to make sure that the selection includes the character at the specified index.

7. select_clear(): Clears the selection. If there isn't currently a selection, has no effect.

8. select_from ( index ): Sets the ANCHOR index position to the character selected by index, and selects that character.

9. select_present(): If there is a selection, returns true, else returns false.

10. select_range ( start, end ): Sets the selection under program control. Selects the text starting at the start index, up to but not including the character at the end index. The start position must be before the end position.

11. select_to ( index ): Selects all the text from the ANCHOR position up to but not including the character at the given index.

12. xview ( index ): This method is useful in linking the Entry widget to a horizontal scrollbar.

13. xview_scroll (number, what): Used to scroll the entry horizontally. The what argument must be either UNITS, to scroll by character widths, or PAGES, to scroll by chunks the size of the entry widget. The number is positive to scroll left to right, negative to scroll right to left.

 

 

 

※查詢其他Tkinter GUI元件用法如下※

http://jennaweng0621.pixnet.net/blog/category/6758171

arrow
arrow
    文章標籤
    python tkinter entry
    全站熱搜

    楓綺 發表在 痞客邦 留言(0) 人氣()