Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to realize login and registration function

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use python to achieve login and registration function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use python to achieve login and registration functions"!

1. Case introduction

This example designs a user login and registration module, which uses Tkinter framework to build the interface, mainly using components such as canvas, text box, button and so on. Knowledge points involved: Python Tkinter interface programming, pickle data storage. This example implements the basic user login and registration interactive interface, and provides user information storage and authentication. Pickle is a standard module of the python language. After installing python, it already includes the pickle library and does not need to be installed separately. The pickle module implements basic data serialization and deserialization. Through the serialization operation of the pickle module, the object information running in the program can be saved to the file and stored permanently; through the deserialization operation of the pickle module, the object saved by the last program can be created from the file. This example is intermediate in difficulty and is suitable for users with basic knowledge of Python and Tkinter component programming.

two。 Example effect

3. Example source code import tkinter as tkimport pickleimport tkinter.messageboxfrom PIL import Image, ImageTk # Settings window-the initial matrix window window = tk.Tk () # create a window window.title ('Welcome to login') window.geometry ('450x300') # window size is 300x200 # canvas canvas = tk.Canvas (window, height=200) Width=900) # load picture im = Image.open ("images/01.png") image_file = ImageTk.PhotoImage (im) # image_file = tk.PhotoImage (file='images/01.gif') image= canvas.create_image (100,40, anchor='nw', image=image_file) canvas.pack (side='top') # two text tags Two parts of user name and password: tk.Label (window, text=' username'). Place (Xero100, yaq150) tk.Label (window, text=' cipher') .place (xint100, yaq190) var_usr_name = tk.StringVar () # the contents of the text box Defined as the string type var_usr_name.set ('amoxiang@163.com') # sets the default value var_usr_pwd = tk.StringVar () # the first input box-the one used to enter the user name. # textvariable gets the contents of the text box entry_usr_name = tk.Entry (window, textvariable=var_usr_name) entry_usr_name.place (xwords 160, yearly 150) # the second input box-used to enter the password. Entry_usr_pwd = tk.Entry (window, textvariable=var_usr_pwd, show='*') entry_usr_pwd.place (xylene 160,190) def usr_login (): usr_name = var_usr_name.get () usr_pwd = var_usr_pwd.get () try: with open ('usrs_info.pickle' 'rb') as usr_file: usrs_info = pickle.load (usr_file) except FileNotFoundError: with open (' usrs_info.pickle', 'wb') as usr_file: usrs_info = {' admin': 'admin'} pickle.dump (usrs_info Usr_file) if usr_name in usrs_info: if usr_pwd = = usrs_ info [usr _ name]: tk.messagebox.showinfo (title=' Welcome to', message=usr_name +': please go to your home page Else: tk.messagebox.showinfo (message=' error prompt: wrong password, please try again) else: is_sign_up = tk.messagebox.askyesno ('prompt', 'you haven't registered yet Please register') print (is_sign_up) if is_sign_up: usr_sign_up () # Registration button def usr_sign_up (): def sign_to_Mofan_Python (): np = new_pwd.get () npf = new_pwd_confirm.get () nn = new_name.get () # above is to obtain data Here is to see if you have re-registered with open ('usrs_info.pickle',' rb') as usr_file: exist_usr_info = pickle.load (usr_file) if np! = npf: tk.messagebox.showerror ('error message') The password and confirmation password must be the same') elif nn in exist_usr_info: tk.messagebox.showerror ('error prompt', 'user name is already registered!') Else: exist_usr_ info [nn] = np with open ('usrs_info.pickle',' wb') as usr_file: pickle.dump (exist_usr_info, usr_file) tk.messagebox.showinfo ('Welcome', 'you have successfully registered') window_sign_up.destroy () # after you click to register This window interface will pop up. Window_sign_up = tk.Toplevel (window) window_sign_up.title ('Welcome to register') window_sign_up.geometry ('360x200') # the middle is x, not the * # username box-enter the username box here. New_name = tk.StringVar () new_name.set ('amoxiang@163.com') # sets the default value of tk.Label (window_sign_up, text=' username') .place (x = 10, y = 10) entry_new_name = tk.Entry (window_sign_up, textvariable=new_name) entry_new_name.place (x = 100) New password box-enter the registration password new_pwd = tk.StringVar () tk.Label (window_sign_up, text=' cipher') .place (window_sign_up 10, text=' 50) entry_usr_pwd = tk.Entry (window_sign_up, textvariable=new_pwd, show='*') entry_usr_pwd.place (xcode 100) Window_sign_up 50) # password confirmation box new_pwd_confirm = tk.StringVar () tk.Label (window_sign_up, text=' confirm password') .place (Xerox 10, YBG 90) entry_usr_pwd_confirm = tk.Entry (window_sign_up, textvariable=new_pwd_confirm, show='*') entry_usr_pwd_confirm.place (Xero100, YBN 90) btn_confirm_sign_up = tk.Button (window_sign_up Text=' Note', command=sign_to_Mofan_Python) btn_confirm_sign_up.place (xregister 120, yearly 130) # create the registration and login button btn_login = tk.Button (window, text=' login', command=usr_login) btn_login.place (xan150, yaw230) # use place to process the location information of the button. Btn_sign_up = tk.Button (window, text=' register', command=usr_sign_up) btn_sign_up.place (xylene 250, yearly 230) window.mainloop () so far, I believe you have a deeper understanding of "how to use python to log in and register". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report