In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to move the focus of the tkinter Entry control in python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Code chip: #-*-coding: utf8-*-from tkinter import * # #-tk-class App: def _ init__ (self Master): frame = Frame (master) frame.pack (expand=1) self.e1 = Entry (frame) self.e1.pack () self.e2 = Entry (frame) self.e2.pack () self.e1.bind (", handlerAdaptor (focus_cg,e2=self.e2)) # tk class cannot pass parameters directly Need lambdadef focus_cg (event,e2): e2.focus_set () # focus to e2def handlerAdaptor (fun, * * kwds): # Adapter for event handler, equivalent to mediation, where does that event come from, I also wonder This may be the greatness of python return lambda event,fun=fun,kwds=kwds: fun (event, * * kwds) if _ _ name__ = ='_ _ main__': root = Tk () app=App (root) root.mainloop ()
Bound by the Tkinter event, parameters cannot be passed directly. Global variables can be used.
Methods that do not use global variables: self.btn = Button (frame,text=u'OK', command=lambda: handler (focus_cg,e2=self.e2 123)) event binding: self.e1.bind (", handlerAdaptor (focus_cg,e2=self.e2)) # tk class cannot pass parameters directly, but requires lambdadef focus_cg (event,e2): e2.focus_set () # focus moves to e2def handlerAdaptor (fun, * * kwds): # event handler adapter, equivalent to mediation Where did that event come from? I wonder, maybe this is the greatness of python. Return lambda event,fun=fun,kwds=kwds: fun (event, * * kwds)
Supplement: the basic application of tkinter text box Entry of Python
Introduction to Entry
The so-called text box Entry, usually refers to a single line of text box, in GUI programming, this is the most basic control for input, we can use it to enter a single-line string, if the length of the input string is greater than the width of the text box, the input text will be automatically hidden, resulting in part of the content can not be displayed, you can use the arrow keys to move the mouse cursor to the invisible area.
The text box is limited to single-line text, and if you want to handle multiline text, you need to use the text in the control
Its grammatical format:
Entry (parent, options,...)
The first parameter, the parent object, indicates the window in which the text box will be established
The second parameter: options, which is as follows
Parameter meaning borderwidth boundary width defaults to two pixels bd boundary width defaults to two pixels background background color bg background color command when the user changes content, it automatically executes this function cursor the shape exportselection when the mouse cursor moves over the button if you select, the selected string is automatically output to the clipboard if you want to avoid it, you can set exportselection=0foreground foreground color fg foreground color Font glyph height high The unit is the character height highlightbackground when the function button gets focus, the background color highlightcolor when the worker button gets focus, justify when there are multiple lines of text, the alignment of the last line of text relief can be controlled by this text frame default is the background color of relief=FLATselectbackground selected string the boundary width of selectborderwidth selected string is preset the foreground color of 1selectfroeground selected string show display input characters for example, show='*' indicates display asterisk Commonly used to enter password fields state input status is NORMAL by default, while DISABLE indicates that you cannot enter textvariable text variable width width in character width xscrollcommand basic application of scroll bar Entry in X axis
Example: create labels and text boxes, enter names and addresses
Import tkinterroot = tkinter.Tk () label1 = tkinter.Label (root, text= "name:") label2 = tkinter.Label (root, text= "address:") label1.grid (row=0) label2.grid (row=1) entry1 = tkinter.Entry (root) entry2 = tkinter.Entry (root) entry1.grid (row=0, column=1) entry2.grid (row=1, column=1) root.mainloop ()
Set grid (row=0), and the system automatically sets column=0 without setting column=x
Use the show parameter to hide the entered characters
In fact, the Entry control can hide input characters using the show parameter setting, so it is often used when entering passwords.
Example: password input, hidden with *
Import tkinterroot = tkinter.Tk () label1 = tkinter.Label (root, text= "account:") label2 = tkinter.Label (root, text= "password:") label1.grid (row=0) label2.grid (row=1) entry1 = tkinter.Entry (root) entry2 = tkinter.Entry (root, show= "*") entry1.grid (row=0, column=1) entry2.grid (row=1, column=1) root.mainloop () Entry get method
Entry has a get () method, which can be used to get the string contents of the current Entry.
Import tkinterdef getValue (): print ("account {}, password is {}" .format (entry1.get (), entry2.get ()) root = tkinter.Tk () label1 = tkinter.Label (root, text= "account:") label2 = tkinter.Label (root, text= "password:") label1.grid (row=0) label2.grid (row=1) entry1 = tkinter.Entry (root) entry2 = tkinter.Entry (root, show= "*") entry1.grid (row=0, column=1) entry2.grid (row=1 Column=1) buttonLogin = tkinter.Button (root, text= "login", command=getValue) buttonExit = tkinter.Button (root, text= "exit", command=root.quit) buttonLogin.grid (row=2, column=0, sticky=tkinter.W, padx=20) buttonExit.grid (row=2, column=1) root.mainloop ()
We enter the account password and click to log in
Account number is 123456, password is 132456
The console shows the information we need.
The insert () method of Entry
When designing GUI programs, it is often necessary to set up input text by default in the text box in which Entry is established. In Widget controls, you can use the insert (index, s) method to insert strings.
Where s is the inserted string, the string will be inserted in the index position
Example: create default text content
Import tkinterdef getValue (): print ("account {}, password is {}" .format (entry1.get (), entry2.get ()) root = tkinter.Tk () label1 = tkinter.Label (root, text= "account:") label2 = tkinter.Label (root, text= "password:") label1.grid (row=0) label2.grid (row=1) entry1 = tkinter.Entry (root) entry2 = tkinter.Entry (root, show= "*") # default account content entry1.insert (0) "123456") # default password content entry2.insert (0, "123456") entry1.grid (row=0, column=1) entry2.grid (row=1, column=1) buttonLogin = tkinter.Button (root, text= "login", command=getValue) buttonExit = tkinter.Button (root, text= "exit", command=root.quit) buttonLogin.grid (row=2, column=0, sticky=tkinter.W, padx=20) buttonExit.grid (row=2, column=1) root.mainloop () Entry delete () method
In the application of the tkinter module, you can use the delete (first, last=None) method to delete the string from the first character to the last-1 character in the Entry. If you want to delete the entire string, you can use delete (0, END).
If the penultimate one is deleted, we can use the len () method to determine the length, and then subtract the length by one.
Example: click the delete button to clear the contents of the text box
Import tkinterdef getValue (): print ("account {}, password {}" .format (entry1.get (), entry2.get ()) # deletion method def deleteValue (): entry1.delete (0, tkinter.END) entry2.delete (0, tkinter.END) root = tkinter.Tk () label1 = tkinter.Label (root, text= "account:") label2 = tkinter.Label (root) Text= "password:") label1.grid (row=0) label2.grid (row=1) entry1 = tkinter.Entry (root) entry2 = tkinter.Entry (root, show= "*") # default account content entry1.insert (0, "123456") # default password content entry2.insert (0, "123456") entry1.grid (row=0, column=1) entry2.grid (row=1, column=1) buttonLogin = tkinter.Button (root, text= "login", command=getValue) # call the deleted method buttonExit = tkinter.Button (root) Text= "delete", command=deleteValue) buttonLogin.grid (row=2, column=0, sticky=tkinter.W, padx=20) buttonExit.grid (row=2, column=1) root.mainloop ()
Example: delete the last one
Def deleteValue (): len_entry = len (entry1.get ()) entry1.delete (len_entry-1) entry2.delete (0, tkinter.END)
If there is only one parameter, then this parameter will be first, if not last,len_entry-1, it will be deleted from the first one to the end.
Evaluate mathematical expressions using eval ()
Python has a very useful function eval for evaluating mathematical expressions.
This function can directly return the evaluation result of this mathematical expression, its syntax format:
"" expression is a string "" result = eval (expression)
Example:
Import tkinterdef getValue (): try: value = eval (entry.get ()) label2.configure (text= "calculation result:" + str (value)) except: passroot = tkinter.Tk () label = tkinter.Label (root, text= "Please enter the mathematical expression:") label.pack (anchor=tkinter.W) entry = tkinter.Entry (root) entry.pack (pady=5) label2 = tkinter.Label (root) label2.pack () button = tkinter.Button (root, text= "calculate" Command=getValue) button.pack (pady=5) root.mainloop () these are all the contents of the article "how to shift the focus of tkinter Entry controls in python" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.