Home > other >  Python GUI tkinter problem, hope to get your reply and answer, thank you
Python GUI tkinter problem, hope to get your reply and answer, thank you

Time:10-10

We now the question is, can enter the following login interface point to determine the next interface, from figure 1 and figure 2

Figure 1


Figure 2


Below is the code

The import tkinter as tk # using tkinter need the guide into the
The import tkinter. Messagebox
The import pickled

Step # 1, instantiation object, establishes the window window
The window=tk. Tk ()

Step # 2, to name window visual
Window. The title (' Welcome to Hongwei Website ')

Step # 3, set the window size (length * width)
Window. The geometry (' 400 x300 ') # in here is little x

Step # 4, wellcome to load the image
Canvas=tk canvas (window width=400, height=135, bg='green')

='top' canvas. Pack (side)
Tk. Label (window, text='Welcome', the font=(' Arial, 16)). The pack ()

Step # 5, user information
Tk. Label (window, text='User name:' the font=(' Arial, 14)). The place (=10 x, y=170)
Tk. Label (window, text='Password:' the font=(' Arial, 14)). The place (=10 x, y=210)

Step # 6, login user input box entry
# username
Var_usr_name=tk. StringVar ()
Var_usr_name. Set (' [email protected] ')
Entry_usr_name=tk. Entry (window, textvariable=var_usr_name, the font=(' Arial, 14))
Entry_usr_name. Place (x=120, y=175)
# the user password
Var_usr_pwd=tk. StringVar ()
Entry_usr_pwd=tk. Entry (window, textvariable=var_usr_pwd, the font=(' Arial, 14), the show='*')
Entry_usr_pwd. Place (x=120, y=215)


Step # 8, define the user login function
Def usr_login () :
These two lines of code # is access to user input usr_name and usr_pwd
Usr_name=var_usr_name. The get ()
Usr_pwd=var_usr_pwd. The get ()

# set the exception handling here, when we first visit the user information file doesn't exist, so here set the exception handling,
# in the middle of the two lines is our match, the program will input information and the information in the file matching,
Try:
With the open (' usrs_info. Pickles', 'rb') as usr_file:
Usrs_info=pickle. The load (usr_file)
Except FileNotFoundError:
# this is we don't have read the ` usr_file `, the program will create a ` usr_file ` this file, and will be the administrator
# user and password, the user named ` admin ` password for ` admin `,
With the open (' usrs_info. Pickles', 'wb) as usr_file:
Usrs_info={' admin ':' admin '}
Pickle. Dump (usrs_info usr_file)
Usr_file. Close () # must shut down, or pickled. The load () will appear EOFError: Ran out of the input

# if the user name and password and file matching success, can login successfully, and jump out of the popup window how are you? Add your user name,
If usr_name usrs_info in:
If usr_pwd==usrs_info [usr_name] :





Tkinter. Messagebox. Showinfo (title='Welcome', message='How are you? '+ usr_name)
# if user name matching success, and the password input Error, it will pop up 'Error, your password is wrong, try again.'
The else:
Tkinter. Messagebox. Showerror (message='Error, your password is wrong, try again.')
The else: # if it is found that the user name does not exist
Is_sign_up=tkinter. Messagebox. Askyesno (' Welcome! ', 'You have not sign up yet, sign up now? ')
# prompt need not to need to register a new user
If is_sign_up:
Usr_sign_up ()


Step # 9, define the function of user registration
Def usr_sign_up () :
Def sign_to_Hongwei_Website () :
# the following three lines is access to our registration information input by
Np=new_pwd. The get ()
NPF=new_pwd_confirm. The get ()
Nn=new_name. The get ()

# here is open we recorded data file, the registration information read
With the open (' usrs_info. Pickles', 'rb') as usr_file:
Exist_usr_info=pickle. The load (usr_file)
# here is to determine if two Password is not consistent, indicates that the Error, the Password and confirm Password must be the same!
If the np!=NPF:
Tkinter. Messagebox. Showerror (' Error ', 'the Password and confirm Password must be the same! ')

# if The user name already in our data file, indicates that The Error, The user has already signed up!
Elif nn in exist_usr_info:
Tkinter. Messagebox. Showerror (' Error ', 'The user has already signed up! ')

# above mistakes, if the input the end, the record registration of input information to a file, and prompt registration Welcome! , You have successfully signed up! , and then destroy the window,
The else:
Exist_usr_info (nn)=np
With the open (' usrs_info. Pickles', 'wb) as usr_file:
Pickle. Dump (exist_usr_info usr_file)
Tkinter. Messagebox. Showinfo (' Welcome ', 'You have successfully signed up! ')
# and then destroy the window,
Window_sign_up. Destroy ()

# define long window on the window
Window_sign_up=tk. Toplevel (Windows)
Window_sign_up. Geometry (' 300 x200)
Window_sign_up. Title (' window 'sign-up)

New_name=tk. StringVar () # will enter registration name assigned to the variable
New_name. Set (' [email protected] ') # will initially displayed as' [email protected] '
Tk. Label (window_sign_up, text='User name:'). The place (=10 x, y=10) # will ` User name: ` placed in coordinates (10, 10),
Entry_new_name=tk. Entry (window_sign_up, textvariable=new_name) # to create a login name ` Entry `, variable for ` new_name `
Entry_new_name. Place (x=130, y=10) # ` entry ` placed in coordinates (150, 10).

New_pwd=tk. StringVar ()
Tk. Label (window_sign_up, text='Password:'). The place (=10 x, y=50)
Entry_usr_pwd=tk. Entry (window_sign_up textvariable=new_pwd, show='*')
Entry_usr_pwd. Place (x=130, y=50)

New_pwd_confirm=tk. StringVar ()
Tk. Label (window_sign_up, text='Confirm password:'). The place (=10 x, y=90)
Entry_usr_pwd_confirm=tk. Entry (window_sign_up textvariable=new_pwd_confirm, show='*')
Entry_usr_pwd_confirm. Place (x=130, y=90)

# the following sign_to_Hongwei_Website
Btn_comfirm_sign_up=tk. The Button (window_sign_up, text='sign-up' command=sign_to_Hongwei_Website)
Btn_comfirm_sign_up. Place (x=180, y=120)


Step # 7, the login and sign up button
Btn_login=tk. The Button (window, text='Login', the command=usr_login)
Btn_login. Place (x=120, y=240)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related