import tkinter as tk
import tkinter.messagebox
import random
"""
初始化
"""
generate_num = random.randint(0,10)
times = 0
root = tk.Tk()
root.geometry("300x180")
root.title("game")
admin1 = tk.StringVar()
pwd1 = tk.StringVar()
f2_entry = tk.Entry()
"""
设计转化页面的函数
"""
def creat_game():
global guess_num1
guess_num1 = tk.StringVar()
frame2 = tk.Frame(root)
frame2.pack()
tip_label = tk.Label(frame2,text = "游戏开始!")
tip_label.pack(pady = 10)
global f2_entry
f2_entry = tk.Entry(textvariable=guess_num1)
f2_entry.pack(pady = 10)
play_button = tk.Button(frame2,text = "确认!",command = play)
play_button.pack(pady = 10)
tip_label.config(text = "")
"""
设计开始游戏按钮的命令
"""
def play():
global times
times += 1
guess_num = guess_num1.get()
if guess_num == str(generate_num):
s = f"bigo,正确答案是{generate_num},你用了{times}次机会"
elif int(guess_num) > generate_num:
s = "猜的稍微大了一点"
elif int(guess_num) < generate_num:
s = "猜的稍微小了一点"
tkinter.messagebox.showinfo("结果!", s)
"""
设计第一个登录界面的按钮命令函数
"""
def login():
admin = admin1.get()
pwd = pwd1.get()
if admin == "111" and pwd == "111":
tkinter.messagebox.showinfo("正常" , "登录成功!")
frame1.destroy()
creat_game()
else:
tkinter.messagebox.showerror("异常","账号或密码错误!")
"""
设计一下组件
"""
frame1 = tk.Frame(root)
frame1.pack()
f1_entry1 = tk.Entry(frame1,textvariable = admin1)
f1_entry1.pack(pady = 10)
f1_entry2 = tk.Entry(frame1,show = "*",textvariable = pwd1)
f1_entry2.pack(pady = 10)
f1_label = tk.Label(frame1,text = "请分别输入您的用户名和密码")
f1_label.pack(pady = 10)
f1_Button = tk.Button(frame1,text = "登录", command = login)
f1_Button.pack(pady = 10)
root.mainloop()
统计专业 在自学python界面设计这块,写了个用户登录猜数字的小游戏,算是给自己锻炼和总结一下吧,希望有大佬看到指点一下
最新推荐文章于 2024-11-09 21:51:27 发布