利用tkinter我们可以做一个简单的猜数字游戏~很好玩哈哈哈
# 王杰安 暨南大学
import random
import tkinter
number=random.randint(100,999)
num=0
maxnum=999
minnum=100
running=True
def go():
right=tkinter.Label(win,text='',bg='lightblue')
right.pack_forget()
global num
global maxnum
global minnum
global running
guess=content.get()
num +=1
text='第%d次尝试'%(num)
tkinter.Label(win,text=text,bg='lightblue').place(x=10,y=60)
try:
if int(guess)==number:
tkinter.Label(win,text='你猜对了',width=20,bg='lightblue').place(x=10,y=80)
elif int(guess)>number:
tkinter.Label(win,text='你猜的太大了',width=20,bg='lightblue').place(x=10,y=80)
elif int(guess)<number:
tkinter.Label(win,text='你猜的太小了',width=20,bg='lightblue').place(x=10,y=80)
except:
tkinter.Label(win,text='请输入正确的整数哟~',width=20,bg='lightblue').place(x=10,y=80)
print('请输入正确的整数哟~')
def reset():
global number
global num
#print('#------------------新的一局猜数字游戏------------------------#')
number=random.randint(100,999)
num=0
tkinter.Label(win,text=' ',bg='lightblue').place(x=10,y=60)
tkinter.Label(win,text=' ',width=18,bg='lightblue').place(x=10,y=80)
tkinter.Label(win,text=' ',width=18,bg='lightblue').place(x=130,y=0)
def answer():
global number
tkinter.Label(win,text='正确答案是:%d'%(number),width=18,bg='lightblue').place(x=130,y=0)
win=tkinter.Tk(className='王杰安——猜数字游戏^v^')
win.geometry('400x100')
win['background']='lightblue'
tkinter.Label(win,text='请输入100到999的整数:',bg='lightblue').place(x=0,y=30)
content=tkinter.Entry(win,width=30,bg='white',fg='red')
content.place(x=150,y=30)
button=tkinter.Button(win,text='确认答案',command=go)
button.place(x=230,y=55)
replay=tkinter.Button(win,text='开启新局',command=reset)
replay.place(x=150,y=55)
answer=tkinter.Button(win,text='公布答案',command=answer)
answer.place(x=310,y=55)
开始我们的游戏,在框内输入猜测的数值即可~
第一次猜测过后,提示我们猜的太小,点击公布答案可以看到答案
直接抄答案~猜中啦!哈哈哈哈