Python learning notes - Tkinter_ 04 entry (single line input box)

Keywords: WPF linq p2p

1, Properties of the Entry field

 

About verification details

reference resources: https://blog.csdn.net/qq_41556318/article/details/85108328

Because I have checked a lot of materials, many of them are not explained clearly here, so they are listed separately and explained in detail.

The Entry component supports the verification of the legitimacy of the input content. For example, if you are required to enter a number and you enter a letter, it is illegal. To realize this function, you need to set the validate, validatecommand and invalidcommand options.

The "switch" that enables validation first is the validate option. The values that can be set for this option are:

valuemeaning
'focus'Validate when the Entry component gains or loses focus
'focusin'Validate when the Entry component gets the focus
'focusout'Validate when the Entry component loses focus
'key'Verify when the input box is edited
'all'Verify when any of the above conditions occurs
'none'1. Turn off the verification function
2. This option is set by default (that is, authentication is not enabled)
3. Note that it is' None 'of the string, not None

The second is to specify a validation function for the validatcommand option, which can only return True or False to indicate the validation result. Generally, the validation function only needs to know the contents of the input box, and the string can be obtained through the get() method of the Entry component.

In the following example, when "CSDN" is entered in the first input box and the focus is transferred to the second input box through Tab or mouse, the verification function is successfully triggered:

import tkinter as tk
 
master = tk.Tk()
 
def test():
    if e1.get() == "CSDN":
        print("correct!")
        return True
    else:
        print("Wrong!")
        e1.delete(0, "end")
        return False
 
v = tk.StringVar()
 
e1 = tk.Entry(master, textvariable=v, validate="focusout", validatecommand=test)
e2 = tk.Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)
 
master.mainloop()

Then, the function specified by the invalidcommand option is called only when the return value of validatcommand is False.

In the following example, enter "csdn" in the first input box and shift the focus to the second input box through the Tab key. The verification function specified by validatecommand is triggered and returns False, and then the invalidcommand is triggered:

import tkinter as tk
 
master = tk.Tk()
 
def test():
    if e1.get() == "CSDN":
        print("correct!")
        return True
    else:
        print("Wrong!")
        e1.delete(0, "end")
        return False
 
def test2():
    print("I was called......")
    return True
 
v = tk.StringVar()
 
e1 = tk.Entry(master, textvariable=v, validate="focusout", validatecommand=test, invalidcommand=test2)
e2 = tk.Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)
 
master.mainloop()

2, Method of Entry input box

1.get() and delete()

from tkinter import *

#=========1. Window settings===========
root = Tk()
#=========2. Layout components===========

entry = Entry(root)
entry.grid(row=0,column=0)

b = Button(root,text='obtain')
b.grid(row=0,column=1)

b2 = Button(root,text='empty')
b2.grid(row=0,column=3)
#=========3. Events===========
def getData(e):
    print('Get data:',entry.get())

def deleteData(e):
    print('Cleared')
    entry.delete(0,'end')

b.bind('<Button-1>',getData)
b2.bind('<Button-1>',deleteData)

root.mainloop()#Prevent windows from closing

  2. insert ()

from tkinter import *

#=========1. Window settings===========
root = Tk()
#=========2. Layout components===========

entry = Entry(root)
entry.grid(row=0,column=0)

b = Button(root,text='obtain')
b.grid(row=0,column=1)

b2 = Button(root,text='empty')
b2.grid(row=0,column=3)

b3 = Button(root,text='insert')
b3.grid(row=0,column=4)
#=========3. Events===========
def getData(e):
    print('Get data:',entry.get())

def deleteData(e):
    print('Cleared')
    entry.delete(0,'end')

def InsertData(e):
    print('insert')
    entry.insert(3,'===')

b.bind('<Button-1>',getData)
b2.bind('<Button-1>',deleteData)
b3.bind('<Button-1>',InsertData)
root.mainloop()#Prevent windows from closing




 

 

Posted by RobinTibbs on Fri, 03 Dec 2021 00:52:16 -0800