【转载】Python GUI编程 Tkinter Entry

转自http://www.yiibai.com/python/python_tk_entry.html

Entry定义:

用于接受用户Entry小窗口部件单行文本字符串.

  • 如果你想显示多行文本可以编辑,那么你应该使用文本(text)部件.

  • 如果你想显示一个或多个不能由用户修改的行文本,那么你应该使用标签(Label)部件.

语法:

这里是一个简单的语法来创建这个widget:

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

参数:

  • master: 这代表了父窗口.

  • options:下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
bg 背后的标签和指标显示正常的背景颜色.
bd 边界周围的指标的大小。默认是2个像素.
command A procedure to be called every time the user changes the state of this checkbutton.
cursor If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the checkbutton.
font The font used for the text.
exportselection By default, if you select text within an Entry widget, it is automatically exported to the clipboard. To avoid this exportation, use exportselection=0.
fg The color used to render the text.
highlightcolor The color of the focus highlight when the checkbutton has the focus.
justify If the text contains multiple lines, this option controls how the text is justified: CENTER, LEFT, or RIGHT.
relief With the default value, relief=FLAT, the checkbutton does not stand out from its background. You may set this option to any of the other styles
selectbackground The background color to use displaying selected text.
selectborderwidth The width of the border to use around selected text. The default is one pixel.
selectforeground The foreground (text) color of selected text.
show Normally, the characters that the user types appear in the entry. To make a .password. entry that echoes each character as an asterisk, set show="*".
state The default is state=NORMAL, but you can use state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE.
textvariable In order to be able to retrieve the current text from your entry widget, you must set this option to an instance of the StringVar class.
width The default width of a checkbutton is determined by the size of the displayed image or text. You can set this option to a number of characters and the checkbutton will always have room for that many characters.
xscrollcommand 如果你希望用户往往会进入多个widget的屏幕大小的文字,你可以链接进入一个滚动部件.

方法:

以下是这个小工具的常用方法:

MedthodDescription
delete ( first, last=None ) 删除字符的部件,在指标之一,但不包括在最后位置的字符开始。如果第二个参数被忽略,只有在单个字符的位置被删除.
get() Returns the entry's current text as a string.
icursor ( index ) Set the insertion cursor just before the character at the given index.
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.
insert ( index, s ) Inserts string s before the character at the given index.
select_adjust ( index ) This method is used to make sure that the selection includes the character at the specified index.
select_clear() Clears the selection. If there isn't currently a selection, has no effect.
select_from ( index ) Sets the ANCHOR index position to the character selected by index, and selects that character.
select_present() If there is a selection, returns true, else returns false.
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.
select_to ( index ) Selects all the text from the ANCHOR position up to but not including the character at the given index.
xview ( index ) This method is useful in linking the Entry widget to a horizontal scrollbar.
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.

语法:

自行尝试下面的例子:

from Tkinter import *

top = Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)

E1.pack(side = RIGHT)

top.mainloop()

这将产生以下结果:

 

posted @ 2017-09-06 17:02  IcarusYu  阅读(244)  评论(0编辑  收藏  举报