In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to limit input numbers in tkinter entry text box". In daily operation, I believe many people have doubts about how to limit input numbers in tkinter entry text box. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to limit input numbers in tkinter entry text box". Next, please follow the editor to study!
Code:
From tkinter import * from tkinter import messageboxroot=Tk () def com (): try: float (e1.get ()) # gets the value of E1 and converts it to a floating point number If the exception l1.config (text=e1.get ()) except: messagebox.showwarning ('warning', 'please enter a number') e1=Entry (root) e1.pack () Button (root,text=' get', command=com) .pack () l1=Label (root,text=' can only be numeric') l1.pack () mainloop ()
Add: python Entry text box can only enter numbers or limit the display of numbers
First show you the code, show respect for your urgent ideas, if you need it urgently, take it directly, and then come back and look at the follow-up if you don't understand. I try to make it clear that I am also a rookie, and I can only write according to my own wishes. if you have any additions or modifications, please contact me, and please don't trust me completely. I don't like to lead you astray!
Initialize TK () from tkinter import * from PIL import Image,ImageTkfrom tkinter import messagebox# before drawing the import tkinter# interface to open the specified picture file Zoom to the specified size def get_image (filename,width,height): im = Image.open (filename). Resize ((width,height)) return ImageTk.PhotoImage (im) win = tkinter.Tk () # initialize TK () create main window # set title win.title ("at most and") # setup interface launches def center_window in the middle (w H): # get the height and width of the screen ws = win.winfo_screenwidth () hs = win.winfo_screenheight () # calculate the x ws/2 y position x = (ws/2)-(wbank 2) y = (hs/2)-(hmp 2) win.geometry ("% dx%d+%d+%d"% Y) # set position and size center_window (300200) # set interface size win.resizable (False,False) # fixed the size of its window can also be allowed to change the size Change to TRUE and you can # create the canvas and set the picture to be displayed Add the canvas to the application window canvas_win = tkinter.Canvas (win,width=300,height=200) im_win = get_image ('kitten startup interface .jpg', 300200) # set the size of the picture canvas_win.create_image (150jpg 100SQL imageimageimage winning) canvas_win.pack () # set icon win.iconbitmap (".\ bitbug_favicon.ico") # set text can only enter numeric def test (content): # if you don't add = "" You will find that you can't finish deleting it. There is always one numeric isdigit function left: the isdigit function method detects whether a string is made up of only numbers. If content.isdigit () or content = = "": return True else: return Falsedef number (): int## displays the text box as the input box, and the tag as the message lable1 = Label (win,text= "account:",) lable1.place (x, 60, StringVar, 30) v = StringVar () # tracks the change in the value of the variable To ensure that the value changes can be displayed on the interface at any time test_cmd = win.register (test) # need to wrap the function, the necessary textEntry1 = Entry (win,bd=0,font= ("Chinese Color Cloud", 12), relief=RIDGE,bg= "lightcyan", textvariable = v, # text box value, is a StringVar () object In this way, validate = "key" can be updated with StringVar. When there is any change in #, the transfer of validatecommand will be affected by the following 'Key'. Similar to keyboard monitoring, if it is replaced by "' focusout'", the cursor validatecommand = (test_cmd,'% P') #% P represents the real-time content of the input box #% P means that the value of the input box is allowed to change. The value is valid. This value is the current text box content #% v (different lowercase and uppercase) The current validate value of #% W represents the name of the component) textEntry1.place (Xerox 100 Grammar 30 journal widthpieces 130 legends 25) lable2 = Label (win,text= "password:") lable2.place (Xerox 60LING 60) textEntry2 = Entry (win,bd=0,font= ("Chinese color cloud"), relief=RIDGE,bg= "lightcyan") textEntry2.place (Xerox 100Cai Yun 60J widthpieces 130) win.mainloop () I would like to introduce several functions: 1, StringVar () function:
Track changes in the values of variables to ensure that changes in values can be displayed on the interface at any time. Through my search, this function is stored in the Tk module, but I haven't found anything else. This is to update the contents of the text box at any time, so this function needs to be used.
2 、 textvariable:
This function appears in parentheses in Entry, this is in conjunction with StringVar, and then update the content.
3. Validate and validatecommand:
To be honest, I haven't found that these two functions can be used separately. One is similar to a driver, and the other is similar to a driven command. Validate drives validatecommand, or calls.
Of course, validate can have many values. I will say that one of them is equal to Key, that is, the key in the code is for keyboard listening, and the other is that focusout belongs to cursor monitoring. Whenever there is an action, the validatecommand function will be called. It has two variables: one is to call another function, and the other is to call another function:
% P indicates that the value is valid when the value of the input box is allowed to change. This value is the content of the current text box
% v (different lowercase and uppercase), the current validate value
% W represents the name of the component
4. Isdigit function:
The isdigit function method detects whether a string consists only of numbers.
5. Win.register (test):
There are many explanations for this function.
The first is the python abstract class-register registers the virtual subclass
Second: the register modifier implies that the compiler's corresponding variables will be used frequently and, if possible, its zhi should be saved in the CPU register to speed up its storage. This keyword requests the compiler to store variables in CPU internal registers as much as possible instead of accessing them through memory addressing to improve efficiency.
And we just want him to wrap it up, let its function make a callback, and register the callback function when the program exits, and then do some resource operations in the callback function.
Attached:
Transparency of the frame background is not allowed in TK, for example, I always wanted to make the text or tag background transparent at the beginning, so that it would make the interface more beautiful. I tried several different methods, what to change the background color, etc., but no, the only feasible word is to trouble a little bit, using PS processing, that is, P out the picture, and then give the tag or text box the background, but relative to the trouble.
At this point, the study on "how to limit input numbers in tkinter entry text boxes" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.