The entry of Python

Source: Internet
Author: User
Tags tkinter tutorial

# Tkinter Tutorial Entry # Entry used to enter a single line of text from tkinter import * root = Tk () # Create Entry Entry (root, text= ' input your text her E '). Pack () # The above code is intended to create a entry object and display ' input your text here ' on entry, run this code, and see no text display, # So unlike lable and buttons, The Text property of the entry cannot be set to entry ' 2. Set the initial value in entry, use Textvariable to bind the variable with entry "E = Stringvar () entry = entry (root, Textva riable=e). Pack () e.set (' Input your text here ') ' 3. Set to read-only entry. Another more useful property of entry, which is set to read-only, does not allow the user to change its value. Set the state property to ' readonly ' e2 = stringvar () entry = entry (root, Textvariable=e2) E2.set (' not modify ') Entry.pack () entr y[' state ' = ' readonly ' # actually entry property value can be used also normal/active/disabled, ' readonly ' as Disabled Root.mainloop () [Python] View plain copy ' 4. Set as the password input box #将Entry作为一个密码输入框来使用, which does not display user-entered content values, with specific symbols instead. Use the properties show to specify. ' From tkinter import * root = Tk () e = Stringvar () entry = entry (root, textvariable=e) e.set (' Input your text he Re ') Entry.pack () # Use * To display input content, if preferred can be changed to other characters entry[' show ' = ' * ' # use *#$ to display the input text content forMask in [' * ', ' # ', ' $ ']: E = stringvar () entry = entry (root, textvariable=e) e.set (' Password ') entry.p ACK () entry[' show '] = Mask root.mainloop () [Python] View plain copy "' 5. Verify that the input content is compliant. The Entry component supports validating the legitimacy of the input, such as the requirement to enter a number, which is illegal if you enter a letter.  To implement this functionality, you need to set the validate, Validatecommand, and Invalidcommand options. The first "switch" to enable authentication is the Validate option, which can be set with the following values: Focus: Validating Focusin When the Entry component gains or loses focus: Verify focusout when the Entry component gets focus: When the Entry component loses focus Verify the key: Verify all when the input box is edited: In case of any of the above, verify that the second is to specify a validation function for the Validatecommand option, which can only return True or False to indicate the result of the validation.  In general, the validation function only needs to know the contents of the input box, which can be obtained through the Get () method of the Entry component. Then, the function specified by the Invalidcommand option is called only if the return value of Validatecommand is False. ' From tkinter import * root = Tk () e = Stringvar () def validatetext (): val = Entry.get () if val = =          ' 654321 ': print ("Correct!") Return True Else: delete content,--Delete all contents of the parameter first to the last range (containing first and last)--if the last argument is omitted, means delete the option specified by the first parameter--using delete (0, END) Implement delete all contents of the input box ' ' Entry.delete (0, END) return False def test (): Print (' Invalidcomm And: I was called ') return True entry = entry (root, textvariable=e, validate= ' focusout ', Validatecommand=validatetext,inva lidcommand=test) Entry.pack () entry (root, text= ' sure '). Pack () Root.mainloop () # There are other properties fg/bg/relief/width/height/ Justify/state use the same method as the button, no longer an example. [Python] View plain copy "" Finally, in fact, Tkinter also have hidden skills, but need to cool to trigger, please listen to the small turtle one by one Tao ... Tkinter provides some additional options for the validation function: The extra option means '%d ' operation code: 0 for the delete operation, 1 for the insert operation, 2 for the gain, loss of focus, or the value of the textvariable variable is modified '%i ' 1. When the user attempts to insert or delete an operation, the line selection represents the location of the insertion or deletion (index number) 2. This value is -1 '%P ' 1 if the validation function is called because of the gain, loss of focus, or the value of the textvariable variable being modified. This value is valid for 2 when the value of the input box is allowed to change. The value is the most recent text content '%s ' of the input box, which is the text content '%s ' 1 of the input box before the validation function is called. This value is valid 2 when the INSERT or delete operation triggers the validation function. This option indicates that the text is inserted and deleted the content '%v ' of the component's current validate option value '%v ' 1. The reason for calling the validation function is 2. The value is ' focusin ', ' focusout ', ' key ' or ' forced ' (the value of the variable specified by the Textvariable option is modified) in the name of the component in order to use these options, you can write:%WMmand= (f, s1, S2, ...) Where F is your "cooled" validation function name, S1, S2, S3 These are additional options that are passed as parameters to the F function in turn. We just said that the need to cool before using hidden skills is to call the register () method to wrap the validation function: "From tkinter import * master = Tk () v = stringvar () def Test (content, reason, name): if content = = "Little Turtle": print ("Correct! ") print (content, reason, name) return True else:print (" Error! ") print (content, reason, name) return False testCMD = master.register (test) e1 = Entry (Master, TE Xtvariable=v, validate= "Focusout", validatecommand= (testCMD, '%P ', '%v ', '%W ')) e2 = Entry (Master) e1.pack (padx=10, Pady  =10) E2.pack (padx=10, pady=10) Mainloop ()

  

The entry of Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.