Tkinter Entry控件篇

Entry控件输入检测

例子1转自此链接
例一:

try:
    # python 3.x
    import tkinter as tk
except ImportError:
    # python 2.x
    import Tkinter as tk


class Example(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        # valid percent substitutions (from the Tk entry man page)
        # %d = Type of action (1=insert, 0=delete, -1 for others)
        # %i = index of char string to be inserted/deleted, or -1
        # %P = value of the entry if the edit is allowed
        # %s = value of entry prior to editing
        # %S = the text string being inserted or deleted, if any
        # %v = the type of validation that is currently set
        # %V = the type of validation that triggered the callback
        #      (key, focusin, focusout, forced)
        # %W = the tk name of the widget

        vcmd = (self.register(self.onValidate),
                '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
        self.entry = tk.Entry(self, validate="key", validatecommand=vcmd)
        self.text = tk.Text(self, height=10, width=40)
        self.entry.pack(side="top", fill="x")
        self.text.pack(side="bottom", fill="both", expand=True)

    def onValidate(self, d, i, P, s, S, v, V, W):
        self.text.delete("1.0", "end")
        self.text.insert("end", "OnValidate:\n")
        self.text.insert("end", "d='%s'\n" % d)
        self.text.insert("end", "i='%s'\n" % i)
        self.text.insert("end", "P='%s'\n" % P)
        self.text.insert("end", "s='%s'\n" % s)
        self.text.insert("end", "S='%s'\n" % S)
        self.text.insert("end", "v='%s'\n" % v)
        self.text.insert("end", "V='%s'\n" % V)
        self.text.insert("end", "W='%s'\n" % W)

        # Disallow anything but lowercase letters
        valid = (S.lower() == S)
        if not valid:
            self.bell()
        return valid


if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

在这里插入图片描述

Entry控制输入
import tkinter as tk

def validate_input(new_value):
    valid = new_value .isdigit() and len(new_value) <= 5
    return valid

root = tk.Tk()

validate = root.register(validate_input)
for i in range(10):
    entry = tk.Entry(root, validate="key", validatecommand=(validate, "%P"))
    entry.pack(side="top", fill="x")

root.mainloop()

在这里插入图片描述

模仿textchanged事件
from tkinter import *

class App:
    def __init__(self, master, w=300, h=100, x=600, y=500):
        master.geometry(f'{w}x{h}+{x}+{y}')
        # self.test(master)
        self.EntryYes(master)

    def test(self, master):
        self.var = BooleanVar()
        self.var.set(1)
        Checkbutton(master, text="你喜欢Python吗", variable=self.var).pack()
        Label(master, textvariable=self.var).pack()

    def SetCenter(self, master, w, h):
        mw, mh = master.winfo_screenwidth(), master.winfo_screenheight()
        x, y = int((mw / 2) - (w / 2)), int((mh / 2) - (h / 2))
        master.geometry(f'{w}x{h}+{x}+{y}')

    def EntryYes(self, master):
        def Vcmd(P):
            lb['text'] = P
            return True
        vcmd = (master.register(Vcmd), '%P')
        Entry(master, validate="key", validatecommand=vcmd).pack(fill=X, padx=5)
        lb = Label(master, text='', bg='gray', fg='white')
        lb.pack(fill=X, padx=5)

if __name__ == '__main__':
    root = Tk()
    root.title('Tk-Entry')
    app = App(root)
    root.mainloop()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值