将这个界面再做复杂一下
时间: 2023-09-06 10:07:41 浏览: 40
好的,我可以利用Tkinter来为这个程序添加更多的功能和美化。以下是一个更复杂的示例程序:
```python
import random
import tkinter as tk
class RockPaperScissors:
def __init__(self):
self.window = tk.Tk()
self.window.title("剪刀石头布游戏")
self.window.geometry("400x400")
self.window.resizable(False, False)
self.window.configure(background="#F5F5F5")
self.title_label = tk.Label(self.window, text="剪刀石头布游戏", font=("Arial", 20), bg="#F5F5F5")
self.title_label.pack(pady=10)
self.label = tk.Label(self.window, text="请选择: 剪刀(s), 石头(r), 布(p)", font=("Arial", 16), bg="#F5F5F5")
self.label.pack(pady=10)
self.entry = tk.Entry(self.window, font=("Arial", 16))
self.entry.pack(pady=5)
self.button = tk.Button(self.window, text="确定", font=("Arial", 16), bg="#FFD700", fg="#FFFFFF", activebackground="#FFA500", activeforeground="#FFFFFF", command=self.play)
self.button.pack(pady=10)
self.result_frame = tk.Frame(self.window, bg="#F5F5F5")
self.result_frame.pack(pady=20)
self.result_label = tk.Label(self.result_frame, font=("Arial", 16), bg="#F5F5F5")
self.result_label.pack()
self.history_frame = tk.Frame(self.window, bg="#F5F5F5")
self.history_frame.pack(pady=20)
self.history_label = tk.Label(self.history_frame, text="历史记录", font=("Arial", 16), bg="#F5F5F5")
self.history_label.pack(pady=10)
self.history_text = tk.Text(self.history_frame, font=("Arial", 12), height=10, width=30, state=tk.DISABLED)
self.history_text.pack()
self.reset_button = tk.Button(self.window, text="重置", font=("Arial", 16), bg="#FFD700", fg="#FFFFFF", activebackground="#FFA500", activeforeground="#FFFFFF", command=self.reset)
self.reset_button.pack(pady=10)
self.window.mainloop()
def play(self):
player_choice = self.entry.get().lower()
if player_choice not in ['s', 'r', 'p']:
self.result_label.config(text="无效的选择,请重新输入!")
return
if player_choice == 's':
player_choice_str = "剪刀"
elif player_choice == 'r':
player_choice_str = "石头"
else:
player_choice_str = "布"
computer_choice = random.choice(['s', 'r', 'p'])
if computer_choice == 's':
computer_choice_str = "剪刀"
elif computer_choice == 'r':
computer_choice_str = "石头"
else:
computer_choice_str = "布"
if player_choice == computer_choice:
result_str = "平局!"
elif (player_choice == 's' and computer_choice == 'p') or (player_choice == 'r' and computer_choice == 's') or (player_choice == 'p' and computer_choice == 'r'):
result_str = "你赢了!"
else:
result_str = "电脑赢了!"
self.result_label.config(text=f"你选择了{player_choice_str},电脑选择了{computer_choice_str}。\n{result_str}")
history_str = f"你选择了{player_choice_str},电脑选择了{computer_choice_str}。{result_str}\n"
self.history_text.configure(state=tk.NORMAL)
self.history_text.insert(tk.END, history_str)
self.history_text.configure(state=tk.DISABLED)
def reset(self):
self.entry.delete(0, tk.END)
self.result_label.config(text="")
self.history_text.configure(state=tk.NORMAL)
self.history_text.delete(1.0, tk.END)
self.history_text.configure(state=tk.DISABLED)
game = RockPaperScissors()
```
这个程序在原有的基础上增加了更多的功能和美化。窗口中添加了一个标题、背景颜色和禁止调整大小。按钮和标签的颜色也进行了修改,并添加了鼠标悬停效果。历史记录使用了一个文本框来显示,当内容过多时可以滚动查看。重置按钮可以清空文本框和标签,重新开始游戏。你可以根据自己的需要进行修改和美化。