简介:
在井子棋的基础上,我们改进了登录界面。允许大量玩家注册!
# -*- coding: utf-8 -*- import tkinter as tk import tkinter.messagebox import pickle import random # 窗口 window = tk.Tk() window.title('欢迎进入python') window.geometry('450x200') # 画布放置图片 # canvas=tk.Canvas(window,height=300,width=500) # imagefile=tk.PhotoImage(file='qm.png') # image=canvas.create_image(0,0,anchor='nw',image=imagefile) # canvas.pack(side='top') # 标签 用户名密码 Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数 Verification_Code = str(Verification_Code)#把类型转换为str型 print(type(Verification_Code)) tk.Label(window, text='用户名:').place(x=100, y=30) tk.Label(window, text='密码:').place(x=100, y=70) tk.Label(window, text='验证码').place(x=100, y=110) tk.Label(window, text=Verification_Code).place(x=320, y=110) # 用户名输入框 var_usr_name = tk.StringVar() entry_usr_name = tk.Entry(window, textvariable=var_usr_name) entry_usr_name.place(x=160, y=30) # 密码输入框 var_usr_pwd = tk.StringVar() entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*') entry_usr_pwd.place(x=160, y=70) #验证码输入框 var_usr_yzm = tk.StringVar() entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm) entry_usr_yzm.place(x=160, y=110) # 登录函数 def usr_log_in(): # 输入框获取用户名密码 usr_name = var_usr_name.get() usr_pwd = var_usr_pwd.get() usr_yzm = var_usr_yzm.get() #测试类型 print(type(usr_yzm),type(Verification_Code)) # 从本地字典获取用户信息,如果没有则新建本地数据库 try: with open('usr_info.pickle', 'rb') as usr_file: usrs_info = pickle.load(usr_file) except FileNotFoundError: with open('usr_info.pickle', 'wb') as usr_file: usrs_info = {'admin': 'admin'} pickle.dump(usrs_info, usr_file) # 判断验证码是否正确用户名和密码是否匹配 if usr_yzm == Verification_Code: if usr_name in usrs_info: if usr_pwd == usrs_info[usr_name]: tk.messagebox.showinfo(title='welcome', message='欢迎您:' + usr_name) else: tk.messagebox.showerror(message='密码错误') # 用户名密码不能为空 elif usr_name == '' or usr_pwd == '': tk.messagebox.showerror(message='用户名或密码为空') # 不在数据库中弹出是否注册的框 else: is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册') if is_signup: usr_sign_up() elif usr_yzm == '': tk.messagebox.showerror(message='验证码不能为空') else: tk.messagebox.showerror(message='验证码有误!') # 注册函数 def usr_sign_up(): # 确认注册时的相应函数 def signtowcg(): # 获取输入框内的内容 nn = new_name.get() np = new_pwd.get() npf = new_pwd_confirm.get() # 本地加载已有用户信息,如果没有则已有用户信息为空 try: with open('usr_info.pickle', 'rb') as usr_file: exist_usr_info = pickle.load(usr_file) except FileNotFoundError: exist_usr_info = {} # 检查用户名存在、密码为空、密码前后不一致 if nn in exist_usr_info: tk.messagebox.showerror('错误', '用户名已存在') elif np == '' or nn == '': tk.messagebox.showerror('错误', '用户名或密码为空') elif np != npf: tk.messagebox.showerror('错误', '密码前后不一致') # 注册信息没有问题则将用户名密码写入数据库 else: exist_usr_info[nn] = np with open('usr_info.pickle', 'wb') as usr_file: pickle.dump(exist_usr_info, usr_file) tk.messagebox.showinfo('欢迎', '注册成功') # 注册成功关闭注册框 window_sign_up.destroy() # 新建注册界面 window_sign_up = tk.Toplevel(window) window_sign_up.geometry('350x200') window_sign_up.title('注册') # 用户名变量及标签、输入框 new_name = tk.StringVar() tk.Label(window_sign_up, text='用户名:').place(x=10, y=10) tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10) # 密码变量及标签、输入框 new_pwd = tk.StringVar() tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50) tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50) # 重复密码变量及标签、输入框 new_pwd_confirm = tk.StringVar() tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90) tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90) # 确认注册按钮及位置 bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册', command=signtowcg) bt_confirm_sign_up.place(x=150, y=130) # 退出的函数 def usr_sign_quit(): window.destroy() # 登录 注册按钮 bt_login = tk.Button(window, text='登录', command=usr_log_in) bt_login.place(x=140, y=150) bt_logup = tk.Button(window, text='注册', command=usr_sign_up) bt_logup.place(x=210, y=150) bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit) bt_logquit.place(x=280, y=150) # 主循环 window.mainloop() #井字棋: from tkinter import * import tkinter.messagebox as msg root = Tk() root.title('TIC-TAC-TOE---Project Gurukul') # labels Label(root, text="player1 : X", font="times 15").grid(row=0, column=1) Label(root, text="player2 : O", font="times 15").grid(row=0, column=2) digits = [1, 2, 3, 4, 5, 6, 7, 8, 9] # for player1 sign = X and for player2 sign= Y mark = '' # counting the no. of click count = 0 panels = ["panel"] * 10 def win(panels, sign): return ((panels[1] == panels[2] == panels[3] == sign) or (panels[1] == panels[4] == panels[7] == sign) or (panels[1] == panels[5] == panels[9] == sign) or (panels[2] == panels[5] == panels[8] == sign) or (panels[3] == panels[6] == panels[9] == sign) or (panels[3] == panels[5] == panels[7] == sign) or (panels[4] == panels[5] == panels[6] == sign) or (panels[7] == panels[8] == panels[9] == sign)) def checker(digit): global count, mark, digits # Check which button clicked if digit == 1 and digit in digits: digits.remove(digit) ##player1 will play if the value of count is even and for odd player2 will play if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button1.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("一号选手获胜") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("二号选手获胜") root.destroy() if digit == 2 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button2.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 3 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button3.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 4 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button4.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 5 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button5.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 6 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button6.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 7 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button7.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 8 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button8.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() if digit == 9 and digit in digits: digits.remove(digit) if count % 2 == 0: mark = 'X' panels[digit] = mark elif count % 2 != 0: mark = 'O' panels[digit] = mark button9.config(text=mark) count = count + 1 sign = mark if (win(panels, sign) and sign == 'X'): msg.showinfo("Result", "Player1 wins") root.destroy() elif (win(panels, sign) and sign == 'O'): msg.showinfo("Result", "Player2 wins") root.destroy() ###if count is greater then 8 then the match has been tied if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False): msg.showinfo("Result", "Match Tied") root.destroy() ####define buttons button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1)) button1.grid(row=1, column=1) button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2)) button2.grid(row=1, column=2) button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3)) button3.grid(row=1, column=3) button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4)) button4.grid(row=2, column=1) button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5)) button5.grid(row=2, column=2) button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6)) button6.grid(row=2, column=3) button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7)) button7.grid(row=3, column=1) button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8)) button8.grid(row=3, column=2) button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9)) button9.grid(row=3, column=3) root.mainloop()
还没有评论,来说两句吧...