Tkinter (05) 输入部件 Entry

输入部件的创建及其选项

import tkinter as tk
parent = tk.Tk()
w = tk.Entry(parent, option=value, ...)
选项 说明
bg or background 背景颜色
bd or borderwidth 外框寛度
cursor 在部件上方时, 鼠标的图样
disabledbackground disabled 时, 背景颜色
disabledforeground disabled 时, 前景颜色
exportselection 文本可否复制到剪贴板 0/1
fg or foreground 前景颜色
font 文本字体
highlightbackground 非聚焦时的聚焦颜色
highlightcolor 聚焦时的聚焦颜色
highlightthickness 聚焦厚度,内定 1, 0 则无
insertbackground 插入光标的背景颜色, 内定为黑色
insertborderwidth 插入光标外框寛度
insertofftime 插入光标消失的时间, 内定为 300ms
insertontime 插入光标显现的时间, 内定为 600ms
invalidcommand 验证输入错误回调函数的注册值
insertwidth 插入光标寛度, 内定为 2 图素, 至少为 insertborderwidth 的两倍
justify 文本在输入部件中相对的位置, 内定为 LEFT, RiGHT/CENTER.
readonlybackground readonly 时的背景色
relief 外框花样, 内定为 SUNKEN
selectbackground 被选择文本的背景颜色
selectborderwidth 被选择文本的外框寛度, 内定为 1 图素
selectforeground 被选择文本的前景颜色
show 代替显示的文本, 如密码输入为 ‘*’
state NORMAL, DISABLED, disabled (仍可以复制内容到剪贴板)
takefocus TAB 键在部件是否会循环焦点 0/1
textvariable 输入部件中的文本内容, 必须使用 StringVar()
validate 指定何时使用回调函数验证
validatecommand 验证输入回调函数
width 部件字寛, 内定为 20 个标准字寛
xscrollcommand 水平滚动条 scrollbar.set () 方法

方法及说明

方法 说明
delete(first, last=None) 删除first ~ last(不含)的文本, last None 则只有在 first的一个字
get() 返回输入件中的文本
icursor(index) 设光标的index之前的位置
index(index) 如果文本过长, 无法完全显示, 用来设置最左的可显示位置
insert(index, s) index前插入字符串s
scan_dragto(x) 鼠标键按下事件处理程序中已标记位置时,鼠标滚动事件处理程序拖弋卷动部件,x为位置
scan_mark(x) 鼠标键按下事件处理程序中标记位置x
select_adjust(index) 调整文本选择区以包含索引index
select_clear() 去除选择
select_from(index) 设置选择区的起始索引处index
select_present() 目前是否有选择区
select_range(start, end) 选择start ~ end(不含)的文本
select_to(index) 选择ANCHOR ~ index(不含)的文本
xview(index) 供水平滚动条scrollbar选项command使用
xview_moveto(f) 移动水平滚动条, f为 0 ~ 1
xview_scroll(number, what) 水平滚动画布number个单位,单位whatUNITSPAGES, UNITS为字寛, PAGES的大小为页, 正值向右, 负值向左

输入验证

回调函数检查输入的文本若合乎要求, 返回 True, 否则返回 False.
在使用回调函数之前, 必须先调用 register(回调函数), 会返回一个字符串 f, 用来调用函数.
使用 validatecommand/invalidcommand 指定回调函数, validate 指定何时使用回调函数.

validate

  1. ‘focus’ - 在失去或得到聚焦时验证
  2. ‘focusin’ - 在得到聚焦时验证
  3. ‘focusout’ - 在失去聚焦时验证
  4. ‘key’ - 在任何输入改变内容时验证
  5. ‘all’ - 在所有情况下验证
  6. ‘none’ - 不作验证

validatecommand (invalidcommand)

  1. 如果回调函数 (注册值 f) 不需要任何参数, 使用 validatecommand = f
  2. 如果回调函数 (注册值 f) 需要一些参数, 使用 validatecommand = (f, s1, s2, …)
    s1, s2, … 使用代替码, 如
    • ‘%d’ 0 删除, 1 插入, -1 失去或得到聚焦, 或 textvariable 内容变动
    • ‘%i’ 删除或插入的起始索引, -1 失去或得到聚焦, 或 textvariable 内容变动
    • ‘%P’ 如果更动是允许的, 文本的内容将会是这个参数.
    • ‘%s’ 文本变更前的内容
    • ‘%S’ 删除或插入的文本
    • ‘%v’ validate 选项的值
    • ‘%V’ 回调函数被调用的原因, ‘focusin’, ‘focusout’, ‘key’, ‘forced’ textvariable 内容变动
    • ‘%W’ 部件名

范例视窗及代码

Tkinter (05) 输入部件 Entry

from tkinter import *

def xscroll_handler(*event):
    action, n = event[0:2]
    if action == 'scroll':
        units = event[2]
        entry.xview_scroll(n, units)
    elif action == 'moveto':
        entry.xview_moveto(n)

def validatecommand(code, index, substring):
    if code == '0':
        label.configure(text=f'Delete "{substring}" at index {index}')
    elif code == '1':
        label.configure(text=f'Insert "{substring}" at index {index}')
    else:
        label.configure(text='Focus in, out or textvariable revised.')
    return 1 if substring.isdigit() else 0

root = Tk()
font = ('Courier New', 16)
check = root.register(validatecommand)
entry = Entry(root, font=font, width=40, bg='darkgreen', fg='white',
    validate='all', validatecommand=(check, '%d', '%i', '%S'))
entry.grid(row=0, sticky=E+W)
x_scrollbar = Scrollbar(root, orient=HORIZONTAL, command=xscroll_handler)
x_scrollbar.grid(row=1, sticky=E+W)
entry['xscrollcommand'] = x_scrollbar.set
label = Label('', width=40, font=font, bg='darkgreen', fg='white')
label.grid(row=2, column=0)

root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接
Jason Yang
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!