Python Tkinter猜灯谜

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python Tkinter猜灯谜

顾木子吖   2022-05-23 我要评论

导语

元宵节,又称上元节、灯节,是春节之后的第一个重要节日。

相传,汉文帝(前179—前157年)为庆祝周勃于正月十五勘平诸吕之乱,每逢此夜,必出宫游玩,与民同乐,在古代,夜同宵,正月又称元月,汉文帝就将正月十五定为元宵节。

随着社会和时代的变迁,元宵节的风俗习惯在不断变化,但至今仍是中国的传统节日。2008年,元宵节选入第二批国家级非物质文化遗产。

对我而言,除了吃元宵、看花灯……还有一件最重要的事情…就是…

猜灯谜!猜灯谜!!猜灯谜!!!猜谜事小,展现聪明才智事大哈哈哈.jpg  开个小玩笑啦~

今天小编就给大家写一款猜灯谜的小游戏一起嗨皮叭~

正文

“猜灯谜”是我国元宵节的一项特色活动,最早是由谜语发展而来。谜语悬之于灯,供人猜射,始于南宋。因为谜语能启迪智慧又饶有兴趣,所以深受社会各阶层欢迎。值此佳节,小编也来凑个热闹,出几个灯谜,给您助助兴!看看您能猜出几个?

1)效果展示

猜灯谜界面——

提醒功能——

回答正确——

2)主程序

from tkinter import messagebox
from PIL import Image, ImageTk
import random
import csv
import tkinter as tk
 
 
 
class LanternRiddles(object):
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("猜灯谜闹元宵")
        self.root.geometry("1200x500")
        self.root.geometry("+100+150")
        self.data = []
        with open('new_data.csv', 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                self.data.append(row)
        self.index = [i for i in range(len(self.data))]
        random.shuffle(self.index)
 
        # 做成背景的装饰
        pic1 = Image.open('pic/bg.jpg').resize((1200, 500))  # 加载图片并调整大小至窗口大小
        pic = ImageTk.PhotoImage(pic1)
        render = tk.Label(self.root, image=pic, compound=tk.CENTER, justify=tk.LEFT)
        render.place(x=0, y=0)
 
        # 标签 and 输入框
        label = tk.Label(self.root, text='输入答案', font=('微软雅黑', 15), fg='black', bg="Magenta")
        label.place(x=0, y=10, width=100, height=40)
        self.entry = tk.Entry(self.root, font=('宋体', 15), width=15, bg="GhostWhite")
        self.entry.place(x=110, y=10, width=150, height=40)  # 设置输入框,输入答案
        # 按钮
        confirm_button = tk.Button(self.root, text='确认', font=('微软雅黑', 15), bg="LightGreen", command=self.check)
        confirm_button.place(x=270, y=10, width=100, height=40)  # 确定按钮
 
        quit_button = tk.Button(self.root, text='退出软件', font=('微软雅黑', 15), bg="LightGreen", command=self.quit)
        quit_button.place(x=800, y=10, width=100, height=40)  # 退出软件
        start_button = tk.Button(self.root, text='开始答题', font=('微软雅黑', 15), bg="LightGreen", command=self.get_next)
        start_button.place(x=0, y=80, width=100, height=40)  # 更换题目
        prompt_button = tk.Button(self.root, text='显示提示', font=('微软雅黑', 15), bg="LightGreen", command=self.show_prompt)
        prompt_button.place(x=650, y=10, width=100, height=40)  # 更换题目
 
        self.riddle = tk.Text(self.root, bg="OrangeRed", fg="dimgray",  font=('微软雅黑', 15))
        self.riddle.place(x=200, y=180, width=300, height=160)  # 显示题目
 
        self.root.mainloop()
 
    def get_next(self):  # 更换题目
        self.riddle.delete('1.0', 'end')  # 清空显示
        index = random.choice(self.index)
        self.index.remove(index)
        self.question = self.data[index][0]
        self.answer = self.data[index][1]
        self.prompt = self.data[index][2]
        self.riddle.insert(tk.END, self.question)
 
    def check(self):  # 验证答案
        reply = self.entry.get()
        if reply in self.answer:
            messagebox.showinfo('提示', '回答正确')
            self.get_next()
            self.entry.delete(0, tk.END)
        else:
            messagebox.showinfo('提示', '回答错误,请重试')
            self.entry.delete(0, tk.END)
 
    def show_prompt(self):  # 显示提示
        messagebox.showinfo('提示', self.prompt)
 
    def quit(self):
        self.root.destroy()
 
 
if __name__ == '__main__':
    LanternRiddles()

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们