用 Tkinter 实现简单的猜数字游戏

背景

TkDocs tutorial 里介绍了 Tkinter\text{Tkinter} Tkinter,其中有 A First (Real) Example 一文,这篇文章里有一个使用 Tkinter\text{Tkinter} Tkinter 生成图形化界面的简单例子。我想在那篇文章的基础上实战一下,于是想到可以写一个简易的猜数字游戏。

正文

代码

完整的代码如下

python 复制代码
from tkinter import *
from tkinter import ttk
import random

lower_bound = 0
upper_bound = 99
target = random.randint(lower_bound, upper_bound)
game_ended = False
guess_cnt = 0

def disable_buttons(low, high):
    for v in range(low, high + 1):
        button_dict[v].state(['disabled'])

def guess(num):
    global guess_cnt
    global game_ended
    global lower_bound
    global upper_bound
    if game_ended:
        return

    guess_cnt += 1
    cnt_message.set("一共猜了%d次" % guess_cnt)
    if num == target:
        result_message.set("您猜对了,目标值是%d" % target)
        game_ended = True
        return
    
    if num < target:
        result_message.set("您猜的数字(%d)比目标值小" % num)
        disable_buttons(lower_bound, num)
        lower_bound = num + 1
    else:
        result_message.set("您猜的数字(%d)比目标值大" % num)
        disable_buttons(num, upper_bound)
        upper_bound = num - 1

root = Tk()
root.title("猜数字")

mainframe = ttk.Frame(root, padding=(3, 3, 12, 12))
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

messsageframe = ttk.Frame(root, padding=(3, 3, 12, 12))
messsageframe.grid(column=0, row=1, sticky=(N, W, E, S))

def add_button(row, col):
    num = row * n + col
    return ttk.Button(mainframe, text=str(num), command=lambda: guess(num))
   
n = 10
button_dict = {}
for row in range(n):
    for col in range(n):
        num = row * n + col
        button = add_button(row, col)
        button.grid(column=col, row=row, sticky=W)
        button_dict[num] = button

result_message = StringVar()
ttk.Label(messsageframe, textvariable=result_message).grid(column=1, row=10, sticky=W)

cnt_message = StringVar()
ttk.Label(messsageframe, textvariable=cnt_message).grid(column=2, row=10, sticky=E)

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
mainframe.columnconfigure(2, weight=1)
for child in mainframe.winfo_children(): 
    child.grid_configure(padx=5, pady=5)

root.bind("<Return>", guess)

root.mainloop()

运行

请将上一小节展示的完整代码保存为 guess_number.py。使用如下命令可以运行 guess_number.py

bash 复制代码
python3 guess_number.py

运行效果如下 ⬇️ 最初一共有 100100 100 个按钮可以点击

我们可以随意选择一个按钮(在下图演示的例子中,我点击了 5151 51,而且目标值比 5151 51 大)来点击,示例效果如下 ⬇️

现在只剩下 52,53,54,⋯ ,9952,53,54,\cdots,99 52,53,54,⋯,99 这些按钮可以点击了。继续操作几次 ⬇️ (下图只是示例效果,您在尝试时,很可能会看到不同的效果)

我猜了 77 7 次,总算猜对了 ⬇️

参考资料

相关推荐
copyer_xyf1 小时前
Python 模块与包的导入导出
前端·后端·python
ice8130331812 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
夜微凉42 小时前
三、Spring
java·后端·spring
copyer_xyf2 小时前
Python venv 虚拟环境
前端·后端·python
林爷万福3 小时前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪
copyer_xyf3 小时前
Python 如何同时做很多事:进程、线程、协程
前端·后端·python
Full Stack Developme3 小时前
Spring Bean 依赖注入
python·spring·log4j
yuhuofei20213 小时前
【Python入门】Python中的元组tuple
python
Full Stack Developme3 小时前
Spring AOP 与 AspectJ
java·后端·spring