# tkinter写的猜数游戏

import tkinter
import random
m=tkinter.Tk()
m.geometry("640x600")
m.title("猜数")
w=tkinter.Frame(m,height=100)
w1=tkinter.Frame(m)
w.pack()
w1.pack()
tkinter.Label(w,text='范围：0-200').pack()
var=tkinter.StringVar()
text=tkinter.Entry(w,textvariable=var)
a=text.get()
text.pack(side=tkinter.TOP)
p=0
n = tkinter.Label(text='开始')
n.pack()

def play():
global  n
n.destroy()
n = tkinter.Label(text='开始')
p = random.randint(0, 200)
tkinter.Button(w1, text='确定', bg='white', command=lambda: judge(p)).grid(row=5, column=1)
tkinter.Button(w1, text='重新开始', bg='white', command=play).grid(row=5, column=2)

def judge(b):
a = text.get()
global n
try:
a=int(a)
if a == b:
n.destroy()
n=tkinter.Label(text='{},猜对了,点重新开始，重新开始'.format(a))
n.pack()
elif a > b:
n.destroy()
n=tkinter.Label(text='{},猜大了'.format(a))
n.pack()

elif a < b:
n.destroy()
n=tkinter.Label(text='{},猜小了'.format(a))
n.pack()

except ValueError:
tkinter.Label(text='说了猜个数'.format(a)).pack()
var.set('')
tkinter.Button(w1,text='开始',bg='white',command=play).grid(row=5,column=2)
m.mainloop()



©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客