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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to create and use a single-line text input box in Tkinter, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
The text input box is the most commonly used form of input in GUI programming, and Tkinter provides the Entry class for this. First, take a look at the results of the program execution:
The first is to build Entry objects, the same technique, similar results.
# create fontftTimes = Font (family='Times', size=24, weight=BOLD)
# create a label to change state.entry = Entry (root, background= "# a0ffa0", foreground= "# 000000", disabledbackground= "# 7f7f7f", disabledforeground= "# 000000", font=ftTimes, width=32) entry.grid (row=0, column=0, columnspan=2)
Next, build a multiline label object to process the keyboard event:
# create text variable.str_var = StringVar () # create a label to change state.label = Label (root,height=10, justify=LEFT, textvariable=str_var) label.grid (row=1, column=0, columnspan=2)
Next, bind the button for the Entry object and press the event. The content of the code is to convert the information of the event to a text column and then set it to the multi-line label built earlier.
# bind eventdef OnKeyPress (e): print (e) current = str_var.get () if len (current): str_var.set (current +'\ n'+ str (e)) else: str_var.set (str (e)) entry.bind ('', OnKeyPress)
The same transition state button:
# change state function.def change_state (): state= entry.cget ('state') if state=='disabled': entry.config (state='normal') elif state=='normal': entry.config (state='readonly') else: entry.config (state='disabled')
# change state button.Button (root,text= "State", command=change_state) .grid (row=2, column=0, sticky=E+W)
Delete the selected text of the code information is relatively large, a little more detailed description.
# delete selection.def delete_selection (): anchor = entry.index (ANCHOR) if anchor: # there is a selection # current position of the insertion cursor insert = entry.index (INSERT) sel_from = min (anchor, insert) sel_to = max (anchor, insert) # delete the selection. Entry.delete (sel_from, sel_to)
# delete selection button.Button (root,text= "Delete", command=delete_selection) .grid (row=2, column=1, sticky=E+W)
ANCHOR is a constant that indicates the starting position of the selected text, with which we can use the index method to get the index of the first selected character; INSERT is a constant that represents the position of the insertion cursor, using this constant, we can use the index method to get the index of the cursor position. When the user chooses as follows:
The starting index of the selected text is 1, and the index of the cursor position is 6. The user may also choose this:
At this point, the starting index of the selected text is 6, and the index of the cursor position is 1.
In either case, we can delete the selected text by deleting the range from the minimum to the maximum of the two values. Of course, you can actually do the same thing by pressing the delete key, just to demonstrate the use of Entry.
This is the answer to the question about how to create and use a single-line text input box in Tkinter. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.