例程:
import tkinter as tk
import random
# 定义怪兽的属性列表
monster_names = ["火焰兽", "冰霜怪", "雷霆魔", "毒雾妖", "巨石灵", "疾风侠", "暗影魔", "光明圣使", "水精灵", "炎魔"]
monster_colors = ["红色", "蓝色", "绿色", "黑色", "白色", "紫色", "黄色", "橙色", "青色", "粉色"]
monster_abilities = ["喷火", "冰冻", "放电", "隐身", "毒雾攻击", "超强力量", "瞬间移动", "治愈能力", "控制元素", "变形"]
monster_sizes = ["小型", "中型", "大型", "巨型"]
# 创建主窗口
root = tk.Tk()
root.title("随机怪兽生成器")
root.geometry("300x200")
root.configure(bg="lightgray")
# 创建显示怪兽信息的标签
name_label = tk.Label(root, text="怪兽名字:", bg="lightgray")
name_label.pack()
color_label = tk.Label(root, text="怪兽颜色:", bg="lightgray")
color_label.pack()
ability_label = tk.Label(root, text="特殊能力:", bg="lightgray")
ability_label.pack()
size_label = tk.Label(root, text="体型:", bg="lightgray")
size_label.pack()
# 创建生成怪兽的函数
def generate_monster():
name = random.choice(monster_names)
color = random.choice(monster_colors)
ability = random.choice(monster_abilities)
size = random.choice(monster_sizes)
name_label.config(text=f"怪兽名字:{name}")
color_label.config(text=f"怪兽颜色:{color}")
ability_label.config(text=f"特殊能力:{ability}")
size_label.config(text=f"体型:{size}")
# 创建生成按钮
generate_button = tk.Button(root, text="生成", command=generate_monster)
generate_button.pack()
# 运行主事件循环
root.mainloop()
拓展任务:
在之前的随机怪兽生成器基础上,进一步实现当生成怪兽信息时,同时显示一个与怪兽相关的随机图片,以增强应用程序的趣味性和可视化效果。