猜数字tkinter

import random
import tkinter as tk

number = random.randint(1, 100)
num = 0
maxnum = 100
minnum = 1
running = True


def go():
    right = tk.Label(win, text='', bg='lightgreen')
    right.pack_forget()
    global num
    global maxnum
    global minnum
    global running
    guess = content.get()
    num += 1
    text = '第%d次尝试' % (num)
    tk.Label(win, text=text, bg='lightgreen').place(x=10, y=60)
    try:
        if int(guess) == number:
            tk.Label(win, text='你猜对了', width=20, bg='lightgreen').place(x=10, y=80)
        elif int(guess) > number:
            tk.Label(win, text='你猜的太大了', width=20, bg='lightgreen').place(x=10, y=80)
        elif int(guess) < number:
            tk.Label(win, text='你猜的太小了', width=20, bg='lightgreen').place(x=10, y=80)
    except:
        tk.Label(win, text='请输入正确的整数哟~', width=20, bg='lightgreen').place(x=10, y=80)
        print('请输入正确的整数哟~')


def reset():
    global number
    global num
    number = random.randint(1, 100)
    num = 0
    tk.Label(win, text='              ', bg='lightgreen').place(x=10, y=60)
    tk.Label(win, text='         ', width=18, bg='lightgreen').place(x=10, y=80)
    tk.Label(win, text='                  ', width=18, bg='lightgreen').place(x=130, y=0)


def answer():
    global number
    tk.Label(win, text='正确答案是:%d' % (number), width=18, bg='lightgreen').place(x=130, y=0)


win = tk.Tk(className='猜数字游戏^v^')
win.geometry('400x100')
win['background'] = 'lightgreen'
tk.Label(win, text='请输入1到100的整数:', bg='lightgreen').place(x=0, y=30)
content = tk.Entry(win, width=30, bg='white', fg='red')
content.place(x=150, y=30)
button = tk.Button(win, text='确认答案', command=go)
button.place(x=230, y=55)
replay = tk.Button(win, text='重开', command=reset)
replay.place(x=150, y=55)
answer = tk.Button(win, text='公布答案', command=answer)
answer.place(x=310, y=55)

tk.mainloop()
请输入正确的整数哟~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值