今年も大盛り上がりで終わったM-1グランプリ2023ですが、結果発表のところをpythonで作ってみました。
M-1_FinalJudge.py
import tkinter as tk
from tkinter import messagebox
import random
class CardFlipperApp:
def __init__(self, master):
self.master = master
self.master.title("Auto Card Flipper")
self.cards = [1, 2, 3, 4, 5, 6, 7]
self.card_contents = {1: ("山\n田\n邦\n子", "ヤ\n|\nレ\nン\nズ"),
2: ("博\n多\n大\n吉", "令\n和\nロ\nマ\nン"),
3: ("サ\nン\nド\n富\n澤", "ヤ\n|\nレ\nン\nズ"),
4: ("ナ\nイ\nツ\n塙", "令\n和\nロ\nマ\nン"),
5: ("海\n原\nと\nも\nこ", "ヤ\n|\nレ\nン\nズ"),
6: ("中\n川\n家\n礼\n二", "令\n和\nロ\nマ\nン"),
7: ("松\n本\n人\n志", "令\n和\nロ\nマ\nン")}
self.card_labels = []
for card_num in self.cards:
front_text, back_text = self.card_contents[card_num]
card_label = tk.Label(self.master, text=front_text, width=8, height=10, relief="solid", borderwidth=1, justify="center", font=("Helvetica", 10))
card_label.bind("<Button-1>", lambda event, num=card_num: self.flip_card(event, num))
card_label.is_flipped = False
self.card_labels.append(card_label)
self.layout_cards()
self.current_card_index = 0
self.master.after(5000, self.auto_flip)
def layout_cards(self):
for card_label in self.card_labels:
card_label.grid(row=0, column=self.card_labels.index(card_label), padx=5, pady=5)
def auto_flip(self):
card_num = self.cards[self.current_card_index]
front_text, back_text = self.card_contents[card_num]
current_label = self.card_labels[self.current_card_index]
if current_label.is_flipped:
current_label.config(text=front_text, bg="gold" if back_text == "令\n和\nロ\nマ\nン" else "white")
else:
current_label.config(text=back_text, bg="yellow" if back_text == "ヤ\n|\nレ\nン\nズ" else "red")
current_label.is_flipped = not current_label.is_flipped
if not (self.current_card_index == len(self.cards) - 1):
self.current_card_index += 1
self.master.after(2000, self.auto_flip)
def flip_card(self, event, card_num):
card_index = self.cards.index(card_num)
current_label = self.card_labels[card_index]
if current_label.is_flipped:
front_text, back_text = self.card_contents[card_num]
current_label.config(text=front_text, bg="gold" if back_text == "令\n和\nロ\nマ\nン" else "white")
else:
front_text, back_text = self.card_contents[card_num]
current_label.config(text=back_text, bg="yellow" if back_text == "ヤ\n|\nレ\nン\nズ" else "red")
current_label.is_flipped = not current_label.is_flipped
if __name__ == "__main__":
root = tk.Tk()
app = CardFlipperApp(root)
root.mainloop()
今年はよく記事を書けました。
来年も興味あることできれば上げていきます。