1. Display of login interface
Teach you to use python's own tkinter library to realize the simplest login interface. The final effect is as follows
2. Create a login window
First create a window
*Create window code
# Import tkinter Library from tkinter import * # create a window win = Tk() # Set window title win.title('land') # Set window size win.geometry('300x150') # The main loop, with this line of code can continue to display the window win.mainloop()
Realization effect
After completing the above code, run the program, and the effect is like this
3. Set the word "account number" on the interface
After creating the window, you can set the content to be displayed on the window. First, the word "account:" is displayed on the window
*Display the "account" code and write the code before win.mainloop()
# Place "account number:" at the position with x coordinate of 50 and y coordinate of 30 Label(text='account number:').place(x=50, y=30)
Realization effect
After completing the above code, the effect is as follows
coordinate
4. Set the text box for entering account number
Next, set an input box to the right of "account:"
*Enter text box - Code
# Create a text box for entering the account number and place it at the position of x=100 and y=30 on the interface uname = Entry(win) uname.place(x=100, y=30)
*Realization effect
5. Set the text box for entering the password
Next, use the same method to place the word "password" and the text box for entering the password on the interface
*Password text box - Code
# Put the word "password:" in the position of x = 50 and y = 70 Label(text='password:').place(x=50, y=70) # Place the text box for entering the password at x=100, y=70 pwd = Entry(win) pwd.place(x=100, y=70)
Realization effect
6. Set login button
After setting the account and password, there is still a missing button for login
*Set login button - Code
# Set the text "login" in the button and place it at the position of x = 100 and y = 110 Button(text='land').place(x=100, y=110)
*Realization effect
7. Add login logic to the button
After setting the login button, the next step is to add a function to the button to judge whether the entered account and password are correct
*Add login logic - code to the button
# Create the login function, which is called when you click the login button def login(): # uname and pwd are the text boxes just created for entering the account password. You can use the get function to obtain data from the text boxes username = uname.get() password = pwd.get() # Suppose the user name is "abc" and the password is "123" if username == 'abc' and password == '123': print('Login successful') else: print('Wrong account or password') # Bind the login function created with the command parameter in the login button Button(text='land', command=login).place(x=100, y=110)
*Realization effect
After completing the above code, a basic login interface is created. If the entered account and password are correct, the "login success" will be output on the console. Otherwise, the "account or password error" will be output on the console
8. Complete code
# Import tkinter Library from tkinter import * # Set login window win = Tk() win.title('land') win.geometry('300x150') win.resizable(0, 0) # Set account Label(text='account number:').place(x=50, y=30) uname = Entry(win) uname.place(x=100, y=30) # Set password Label(text='password:').place(x=50, y=70) pwd = Entry(win) pwd.place(x=100, y=70) # land def login(): username = uname.get() password = pwd.get() if username == 'abc' and password == '123': print('Login successful') else: print('Wrong account or password') # Login button Button(text='land', command=login).place(x=100, y=110) win.mainloop()
Finally, the most important thing in learning Python is mentality. We are bound to encounter many problems in the process of learning. We may not be able to solve them if we want to break our head. This is normal. Don't rush to deny yourself and doubt yourself. If you have difficulties in learning at the beginning and want to find a python learning and communication environment, you can join our Python skirt, receive learning materials and discuss together, which will save a lot of time and reduce many problems.