In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian introduces in detail for you "Python how to use Tkinter GUI to achieve input verification function", the content is detailed, the steps are clear, and the details are handled properly. I hope this "Python how to use Tkinter GUI to achieve input verification function" article can help you solve your doubts.
1. Brief introduction to Tkinter verification
Tkinter validation depends on three options that are available for any input widget, such as the Entry widget:
Validate: specifies which type of event will trigger validation.
Validatecommand: check whether the data is valid
Invalidcommand: executes when the data is invalid. In other words, if the validate command returns False, it will execute.
1.1 validate command
The validate command can be one of the following string values:
The name describes when the focus' verification widget gains or loses focus focusin' validates whenever the widget gains focus, focusout' verifies when the widget loses focus, key' validates whenever any keystroke changes the contents of the widget, all' verifies focus, focus and key in all the above cases none' closes validation. Default setting 1.2 validatecommand
Validatecommand is a tuple that contains:
Reference to the Tcl/tk function.
Zero or more replacement code specifies the information that triggers the event to be passed to the function.
To get a reference to the Tck/tk function, pass the callable object to the widget.register () method. It returns a string that can be used with the validate command.
The following table shows the replacement codes available for tuples:
The name describes the% d' operation code: 0 indicates an attempt to delete, 1 indicates an attempt to insert, or-1 if a callback is called to focus, focus, or change the "text variable"'% i' when the user attempts to insert or delete text, this parameter will be the index at the beginning of the insert or delete. If the callback is due to focus, focus, or change "textvariable", the parameter will be "- 1"'% P' if change is allowed, the text will have a value of'% s'if the text'% S' in the Entry before the change is called due to insert or delete, this parameter will be the reason for the callback if the current value'% V' of the validate option of the text'% v' widget is inserted or deleted: if the textvariable is changed Then the name of the 'focusin',' focusout', 'key', or one of the' forced''% W' widgets
The following example constructs a validatecommand that uses the self.validate () method and the% P replacement code:
Vcmd = (self.register (self.validate),'% P') 1.3 invalidcommand
Like validatecommand, invalidcommand requires the use of the widget.register () method and replacement code.
The following example returns a tuple that you can pass to the invalidcommand option:
Ivcmd = (self.register (self.on_invalid),) 2. Complete example
Next you will create a form that contains e-mail input. If you enter an invalid e-mail address, it displays an error message and changes the text entered by the e-mail to red. When the focus moves out of the entry, we trigger a validation event.
Import tkinter as tkfrom tkinter import ttkimport reclass App (tk.Tk): def _ init__ (self): super (). _ init__ () self.title ('Tkinter Validation Demo') self.create_widgets () def create_widgets (self): self.columnconfigure (0, weight=1) self.columnconfigure (1, weight=3) self.columnconfigure (2) Weight=1) # label ttk.Label (text='Email:') .grid (row=0, column=0, padx=5, pady=5) # email entry vcmd = (self.register (self.validate),'% P') ivcmd = (self.register (self.on_invalid),) self.email_entry = ttk.Entry (self, width=50) self.email_entry.config (validate='focusout', validatecommand=vcmd, invalidcommand=ivcmd) self.email_entry.grid (row=0 Column=1, columnspan=2, padx=5) self.label_error = ttk.Label (self, foreground='red') self.label_error.grid (row=1, column=1, sticky=tk.W, padx=5) # button self.send_button = ttk.Button (text='Send') .grid (row=0, column=4, padx=5) def show_message (self, error='' Color='black'): self.label_error ['text'] = error self.email_entry [' foreground'] = color def validate (self, value): "Validat the email entry: param value:: return: pattern = r'\ b [A-Za-z0-9.percent percent] + @ [A-Za-z0-9.9 -] +\. [Amurz | Amurz] {2 }\ b'if re.fullmatch (pattern, value) is None: return False self.show_message () return True def on_invalid (self): Show the error message if the data is not valid self.show_message ('Please enter a valid email',' red') if _ _ name__ ='_ _ main__': app = App () app.mainloop ()
How does the sample code work?
The first step is to create a validation command using the self.validate () method and the% P replacement code:
Vcmd = (self.register (self.validate),'% P')
The second step is to create an invalidatecommand using the self.on_invalid method:
Ivcmd = (self.register (self.on_invalid),)
The third step is to configure Entry widgets that use validation, validatecommand, and invalidatecommand:
Self.email_entry.config (validate='focusout', validatecommand=vcmd, invalidcommand=ivcmd)
The fourth step is to define the show_message () method that changes the text of the label_error widget and the text color of the email_entry widget:
Def show_message (self, error='', color='black'): self.label_error ['text'] = error self.email_entry [' foreground'] = color
The fifth step is to define the validate () method that validates the email_entry value.
Def validate (self, value): "Validat the email entry: param value:: return:" pattern = r'\ b [A-Za-z0-9.9% stories -] + @ [A-Za-z0-9.9 -] +\. [Amurz | Amurz] {2,}\ b'if re.fullmatch (pattern, value) is None: return False self.show_message () return True
If the input text is valid, the validate () method returns True, otherwise it returns False. If the input text is a valid e-mail address, call show_message () to hide the error message and set the text color to black.
If the input text is not a valid e-mail address, Tkinter executes the on_invalid () method.
Finally, define the on_invalid () method that displays the error message and set the text color of the email_entry widget to red.
Def on_invalid (self): "" Show the error message if the data is not valid: return: "" self.show_message ('Please enter a valid email',' red'). This article "how to use Tkinter GUI to implement input verification in Python" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.