3、编写GUI界面,模拟抢红包游戏,在界面输入红包大小和抢红包人姓名(用逗号隔开),输出系统当前时间以及每个人抢的红包大小,要求每个人抢的红包最少不能少于0.01元,精确到小数点后两位
时间: 2024-04-04 16:35:57 浏览: 67
好的,我理解了你的要求。这里提供一个Python的GUI界面程序,可以实现你所需要的功能。你可以根据需要进行修改和完善。
```python
import tkinter as tk
import time
import random
class RedEnvelopeGame:
def __init__(self, master):
self.master = master
master.title("Red Envelope Game")
self.label1 = tk.Label(master, text="Red Packet Size:")
self.label1.grid(row=0, column=0)
self.entry1 = tk.Entry(master)
self.entry1.grid(row=0, column=1)
self.label2 = tk.Label(master, text="Player Names:")
self.label2.grid(row=1, column=0)
self.entry2 = tk.Entry(master)
self.entry2.grid(row=1, column=1)
self.button = tk.Button(master, text="Start Game", command=self.start_game)
self.button.grid(row=2, column=0, columnspan=2)
self.text = tk.Text(master, width=50, height=10)
self.text.grid(row=3, column=0, columnspan=2)
def start_game(self):
red_packet_size = float(self.entry1.get())
player_names = self.entry2.get().split(',')
num_players = len(player_names)
if num_players == 0:
self.text.insert(tk.END, "Please enter at least one player name.\n")
return
if red_packet_size <= 0:
self.text.insert(tk.END, "Red packet size should be greater than zero.\n")
return
total_amount = round(red_packet_size, 2)
amounts = [0] * num_players
while total_amount > 0:
i = random.randint(0, num_players - 1)
amount = round(random.uniform(0.01, total_amount), 2)
amounts[i] += amount
total_amount = round(total_amount - amount, 2)
self.text.insert(tk.END, time.strftime("%Y-%m-%d %H:%M:%S") + "\n")
for i in range(num_players):
self.text.insert(tk.END, player_names[i] + ": " + "{:.2f}".format(amounts[i]) + "\n")
root = tk.Tk()
game = RedEnvelopeGame(root)
root.mainloop()
```
使用方法:
1. 运行程序后,在界面上输入红包大小和抢红包人姓名,然后点击“Start Game”按钮。
2. 程序会随机分配每个人抢到的红包金额,并在界面上显示当前时间和每个人抢到的金额。
注意事项:
1. 输入的红包大小必须为正数。
2. 输入的抢红包人姓名必须用逗号隔开。
3. 每个人抢到的红包金额不能少于0.01元,精确到小数点后两位。
4. 程序仅供参考,如有需要,请根据实际需求进行修改和完善。
阅读全文