用户:
安澜仙帝查看:0 回复:0 评论:0 创建时间:2023-07-16T13:10:28
import tkinter as tk
import random as r#导入库
def click(s):
entry.insert(tk.END,s)#点击显示函数
def begin():
global ans
ans=r.randint(1,1000)#开始函数,生成一个1-1000的数
def display_ans():
entry.delete(0,tk.END)#清空
entry.insert(0,ans)#显示答案函数
def count():
el = entry.get()
if el.isdigit():
entry.delete(0,tk.END)
if int(el) > ans:
entry.insert(0,'猜大了')
elif int(el) < ans:
entry.insert(0,'猜小了')
else:
entry.insert(0,'猜对了')#判断函数
def clear():
entry.delete(0, tk.END)#清空函数
window = tk.Tk()
window.title('guess__number')
window.geometry('665x435')#生成一个窗口
entry = tk.Entry(window, width=30, font=('', 24))
entry.grid(row=0, column=0, columnspan=5)
size = 'Arial', 30
for i in range(10):
button = tk.Button(window, text=str(i), width=5, height=2, font=size, command=lambda x=i: click(x))
button.grid(row=i // 5 + 1, column=i % 5, padx=4, pady=4)#生成数字按钮
button = tk.Button(window, text='退出', width=5, height=2, font=size, command=quit)
button.grid(row=3, column=0, padx=4, pady=4)
button = tk.Button(window, text='开始', width=5, height=2, font=size, command=begin)
button.grid(row=3, column=1, padx=4, pady=4)
button = tk.Button(window, text='答案', width=5, height=2, font=size, command=display_ans)
button.grid(row=3, column=2, padx=4, pady=4)
button = tk.Button(window, text='清除', width=5, height=2, font=size, command=clear)
button.grid(row=3, column=3, padx=4, pady=4)
button = tk.Button(window, text='判断', width=5, height=2, font=size, command=count)
button.grid(row=3, column=4, padx=4, pady=4)#生成功能按钮
window.mainloop()