基于Python且不用Pyqt5做出精美石头剪刀布的GUI界面
时间: 2023-11-05 12:30:16 浏览: 134
可以使用Python自带的Tkinter模块来实现石头剪刀布游戏的GUI界面。以下是一个简单的实现示例:
```python
import tkinter as tk
import random
# 定义游戏结果的字典
result_dict = {'石头': {'石头': '平局', '剪刀': '你赢了', '布': '你输了'},
'剪刀': {'石头': '你输了', '剪刀': '平局', '布': '你赢了'},
'布': {'石头': '你赢了', '剪刀': '你输了', '布': '平局'}}
# 定义游戏函数
def game(player_choice):
computer_choice = random.choice(['石头', '剪刀', '布'])
result = result_dict[player_choice][computer_choice]
result_str.set(result)
computer_str.set(computer_choice)
# 创建主窗口
root = tk.Tk()
root.title('石头剪刀布游戏')
# 创建控件
player_str = tk.StringVar()
player_label = tk.Label(root, text='你的选择:', font=('Arial', 14))
player_label.grid(row=0, column=0, padx=10, pady=10)
player_radio1 = tk.Radiobutton(root, text='石头', value='石头', variable=player_str)
player_radio1.grid(row=0, column=1)
player_radio2 = tk.Radiobutton(root, text='剪刀', value='剪刀', variable=player_str)
player_radio2.grid(row=0, column=2)
player_radio3 = tk.Radiobutton(root, text='布', value='布', variable=player_str)
player_radio3.grid(row=0, column=3)
computer_str = tk.StringVar()
computer_label = tk.Label(root, text='电脑选择:', font=('Arial', 14))
computer_label.grid(row=1, column=0, padx=10, pady=10)
computer_choice = tk.Label(root, textvariable=computer_str, font=('Arial', 14))
computer_choice.grid(row=1, column=1, columnspan=3)
result_str = tk.StringVar()
result_label = tk.Label(root, text='游戏结果:', font=('Arial', 14))
result_label.grid(row=2, column=0, padx=10, pady=10)
result = tk.Label(root, textvariable=result_str, font=('Arial', 14))
result.grid(row=2, column=1, columnspan=3)
play_button = tk.Button(root, text='出招', font=('Arial', 14), command=lambda: game(player_str.get()))
play_button.grid(row=3, column=0, columnspan=4, pady=10)
# 进入消息循环
root.mainloop()
```
运行以上代码,即可看到一个简单的石头剪刀布游戏界面。
阅读全文