This is the 24th day of my participation in the August Text Challenge.More challenges in August
Review and
When we use the Nuggets APP for the first time, we will see the following login screen
-
The cute nuggets icon is the Label Label
-
The login Button is Button
-
The email/mobile phone bar and password bar are Entry text boxes
Python Tkinter provides a variety of components, including Label and Button
In this installment, we continue learning about properties and methods related to the Widget subclass Entry single-line text box component
1. Entry/Text overview
Entry is a standard component of Python Tkinter that accepts strings from the user’s keyboard
Entry A single-line text box used to receive a string of components.
- When the entered line exceeds the size of the Entry, the line is broken to the next line.
- It is mainly used in input box scenarios such as filling in forms
2. Common Entry methods
The Entry component provides methods to manipulate strings.
methods | role |
---|---|
delete(first,last=None) | Delete the index value in the text box |
get() | Gets the value of the text box |
icursor(index) | Moves the cursor to the specified index position |
index(index) | Returns the specified index value |
insert(index,s) | Inserts a value into the text box |
select_adjust(index) | Select the value before the specified index and index location |
select_clear() | Clear the text box |
select_from(index) | Set the cursor position by index |
select_present() | Returns True if selected |
select_range(start,end) | Select the value for the specified index location |
select_to(index) | Select the value between the specified index and the cursor |
3. Entry Common attributes
attribute | meaning |
---|---|
height | Sets the height of the textbox. Each increment of the height value adds one line |
width | Sets the width of the textbox, incrementing the value by 1 byte |
insert | Text box to insert data, you can specify the location to insert data |
delete | Delete data in the text box. You can specify the data to be deleted according to the data location |
get | Get the data in the text box, you can specify the data to be obtained by the data location |
relief | Textbox style, set the display effect of controls. Options: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. |
bd | Sets the border size of the text box. The larger the value, the wider the border |
bg | Sets the default background color of the text box |
fg | Sets the default foreground color of the text box, the font color |
font | Text font, text size, text glyphs. Glyph overstrike/italic/bold/underline |
state | The value can be DISABLED or NORMAL. The value cannot be entered in the DISABLED state text box, but can be entered in the NORMAL state |
highlightcolor | Sets the border color of the text box after it is clicked |
highlightthickness | Sets the border size of the text box after it is clicked |
4. Test the cat
📝 Let’s practice using the Entry and Text components:
Entry realizes the login interface as follows:
-
Create Account and Password are Label components
self.lab = Label(self,text = "Account") self.lab.pack() self.lab2 = Label(self, text="Password") self.lab2.pack() Copy the code
-
Accounts and passwords need to be created using the Entry component
-
Account/password values change dynamically, so we use StringVar
- ⭐ StringVar
-
StringVar inherits the Variable type
-
The StringVar variable is bound to the specified component
-
When the value of the StringVar variable changes, the component content changes as well
-
The component content changes, as does the value of the StringVar variable
-
StringVar is then bound using Textvariable of the Entry component
Therefore, the Account/Password input box can be implemented in code
``` self.lab = Label(self,text = "Account") self.lab.pack() v1 = StringVar() self.En = Entry(self,textvariable = v1) self.En.pack() v1.set("admin") print(v1.get()); print(self.En.get()) self.lab2 = Label(self, text="Password") self.lab2.pack() v2 = StringVar() self.En2 = Entry(self, textvariable=v2,show = "*") self.En2.pack() ```Copy the code
-
-
Add login button and bind login event
Button(self,text = "Login",command = self.login).pack() Copy the code
-
Application(Frame) encapsulates the process of creating a component in the CreateWidgetEntry() method, and initializes the Appication instance object using the constructor
-
Finally, the complete implementation code is as follows
from tkinter import * class Application(Frame): def __init__(self,master=None): # super() represents the definition of the parent class, Super ().__init__(master) self.master = master self.pack() self.creatwidgeTentry () def CreatWidgetEntry(self): self.lab = Label(self,text = "Account") self.lab.pack() v1 = StringVar() self.En = Entry(self,textvariable = v1) self.En.pack() v1.set("admin") print(v1.get()); print(self.En.get()) self.lab2 = Label(self, text="Password") self.lab2.pack() v2 = StringVar() self.En2 = Entry(self, textvariable=v2,show = "*") self.En2.pack() Button(self,text = "Login",command = self.login).pack() def login(self): Name = self.en. get() print("Account:"+name) print("Password:" + PDW) print(" ") if name == "Admin" and PDW =="1234567": messagebox.showinfo("Juejin"," login successful ") else: messagebox.showinfo("Juejin", Root = Tk() root. Geometry ("400x130+200+300") root. Title ("MyfirstAPP") app = Application(master=root) root.mainloop()Copy the code
conclusion
In this installment, we will learn about the text Entry field of the Tkinter component. The single-line text field is often used in the login interface for form-filling scenarios such as user name and password
In a work scenario where only one line of input is required, you can choose to use the Entry component, which also provides methods for deleting, modifying, and inserting text
The above is the content of this issue, welcome big guys to point up comments and corrections, see you next time ~ღ(´ ᴗ · ‘) than xin 🌹