19
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonでM-1の最終決戦を作ってみた!

Last updated at Posted at 2023-12-28

今年も大盛り上がりで終わったM-1グランプリ2023ですが、結果発表のところをpythonで作ってみました。

IMG_3816.jpeg

IMG_3817.jpeg

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()

今年はよく記事を書けました。
来年も興味あることできれば上げていきます。

19
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?