Python剪刀石头布的GUI开发
时间: 2023-08-27 08:35:16 浏览: 91
python实现石头剪刀布程序
好的,您可以使用 Python 的 tkinter 模块来开发剪刀石头布的 GUI。以下是一个简单的示例代码:
```python
import tkinter as tk
import random
# 定义剪刀石头布的选项
options = {
0: "剪刀",
1: "石头",
2: "布"
}
# 定义判断胜负的函数
def judge(player, computer):
if player == computer:
return "平局"
elif (player == 0 and computer == 2) or (player == 1 and computer == 0) or (player == 2 and computer == 1):
return "恭喜你,你赢了!"
else:
return "很遗憾,你输了。"
# 定义剪刀石头布的函数
def play(option):
# 获取电脑的选项
computer_option = random.randint(0, 2)
# 判断胜负
result = judge(option, computer_option)
# 更新显示结果的标签
result_label.config(text=result)
# 更新显示电脑选项的标签
computer_option_label.config(text=options[computer_option])
# 创建 GUI 界面
root = tk.Tk()
root.title("剪刀石头布")
# 创建剪刀按钮
scissors_button = tk.Button(root, text="剪刀", command=lambda: play(0))
scissors_button.pack(side="left", padx=10)
# 创建石头按钮
stone_button = tk.Button(root, text="石头", command=lambda: play(1))
stone_button.pack(side="left", padx=10)
# 创建布按钮
cloth_button = tk.Button(root, text="布", command=lambda: play(2))
cloth_button.pack(side="left", padx=10)
# 创建显示电脑选项的标签
computer_option_label = tk.Label(root, text="")
computer_option_label.pack()
# 创建显示结果的标签
result_label = tk.Label(root, text="")
result_label.pack()
# 运行 GUI 界面
root.mainloop()
```
运行代码后,会弹出一个窗口,里面有三个按钮,分别对应剪刀、石头和布。点击按钮后,程序会随机生成电脑的选项,并判断胜负。最后会在窗口中显示电脑的选项和胜负结果。
阅读全文