|
| 1 | +# View more python learning tutorial on my Youtube and Youku channel!!! |
| 2 | + |
| 3 | +# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg |
| 4 | +# Youku video tutorial: http://i.youku.com/pythontutorial |
| 5 | + |
| 6 | +import tkinter as tk |
| 7 | +import pickle |
| 8 | + |
| 9 | +window = tk.Tk() |
| 10 | +window.title('Welcome to Mofan Python') |
| 11 | +window.geometry('450x300') |
| 12 | + |
| 13 | +# welcome image |
| 14 | +canvas = tk.Canvas(window, height=200, width=500) |
| 15 | +image_file = tk.PhotoImage(file='welcome.gif') |
| 16 | +image = canvas.create_image(0,0, anchor='nw', image=image_file) |
| 17 | +canvas.pack(side='top') |
| 18 | + |
| 19 | +# user information |
| 20 | +tk.Label(window, text='User name: ').place(x=50, y= 150) |
| 21 | +tk.Label(window, text='Password: ').place(x=50, y= 190) |
| 22 | + |
| 23 | +var_usr_name = tk.StringVar() |
| 24 | +var_usr_name.set('example@python.com') |
| 25 | +entry_usr_name = tk.Entry(window, textvariable=var_usr_name) |
| 26 | +entry_usr_name.place(x=160, y=150) |
| 27 | +var_usr_pwd = tk.StringVar() |
| 28 | +entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*') |
| 29 | +entry_usr_pwd.place(x=160, y=190) |
| 30 | + |
| 31 | +def usr_login(): |
| 32 | + usr_name = var_usr_name.get() |
| 33 | + usr_pwd = var_usr_pwd.get() |
| 34 | + try: |
| 35 | + with open('usrs_info.pickle', 'rb') as usr_file: |
| 36 | + usrs_info = pickle.load(usr_file) |
| 37 | + except FileNotFoundError: |
| 38 | + with open('usrs_info.pickle', 'wb') as usr_file: |
| 39 | + usrs_info = {'admin': 'admin'} |
| 40 | + pickle.dump(usrs_info, usr_file) |
| 41 | + if usr_name in usrs_info: |
| 42 | + if usr_pwd == usrs_info[usr_name]: |
| 43 | + tk.messagebox.showinfo(title='Welcome', message='How are you? ' + usr_name) |
| 44 | + else: |
| 45 | + tk.messagebox.showerror(message='Error, your password is wrong, try again.') |
| 46 | + else: |
| 47 | + is_sign_up = tk.messagebox.askyesno('Welcome', |
| 48 | + 'You have not signed up yet. Sign up today?') |
| 49 | + if is_sign_up: |
| 50 | + usr_sign_up() |
| 51 | + |
| 52 | +def usr_sign_up(): |
| 53 | + def sign_to_Mofan_Python(): |
| 54 | + np = new_pwd.get() |
| 55 | + npf = new_pwd_confirm.get() |
| 56 | + nn = new_name.get() |
| 57 | + with open('usrs_info.pickle', 'rb') as usr_file: |
| 58 | + exist_usr_info = pickle.load(usr_file) |
| 59 | + if np != npf: |
| 60 | + tk.messagebox.showerror('Error', 'Password and confirm password must be the same!') |
| 61 | + elif nn in exist_usr_info: |
| 62 | + tk.messagebox.showerror('Error', 'The user has already signed up!') |
| 63 | + else: |
| 64 | + exist_usr_info[nn] = np |
| 65 | + with open('usrs_info.pickle', 'wb') as usr_file: |
| 66 | + pickle.dump(exist_usr_info, usr_file) |
| 67 | + tk.messagebox.showinfo('Welcome', 'You have successfully signed up!') |
| 68 | + window_sign_up.destroy() |
| 69 | + window_sign_up = tk.Toplevel(window) |
| 70 | + window_sign_up.geometry('350x200') |
| 71 | + window_sign_up.title('Sign up window') |
| 72 | + |
| 73 | + new_name = tk.StringVar() |
| 74 | + new_name.set('example@python.com') |
| 75 | + tk.Label(window_sign_up, text='User name: ').place(x=10, y= 10) |
| 76 | + entry_new_name = tk.Entry(window_sign_up, textvariable=new_name) |
| 77 | + entry_new_name.place(x=150, y=10) |
| 78 | + |
| 79 | + new_pwd = tk.StringVar() |
| 80 | + tk.Label(window_sign_up, text='Password: ').place(x=10, y=50) |
| 81 | + entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*') |
| 82 | + entry_usr_pwd.place(x=150, y=50) |
| 83 | + |
| 84 | + new_pwd_confirm = tk.StringVar() |
| 85 | + tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y= 90) |
| 86 | + entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*') |
| 87 | + entry_usr_pwd_confirm.place(x=150, y=90) |
| 88 | + |
| 89 | + btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Mofan_Python) |
| 90 | + btn_comfirm_sign_up.place(x=150, y=130) |
| 91 | + |
| 92 | +# login and sign up button |
| 93 | +btn_login = tk.Button(window, text='Login', command=usr_login) |
| 94 | +btn_login.place(x=170, y=230) |
| 95 | +btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up) |
| 96 | +btn_sign_up.place(x=270, y=230) |
| 97 | + |
| 98 | +window.mainloop() |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments