分享一个 GUI全屏倒计时代码

import tkinter as tk
import winsound


class CountdownGUI:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("倒计时")
        self.window.attributes('-fullscreen', True)
        self.window.configure(bg="black")

        self.time_label = tk.Label(self.window, text="倒计时时间(秒):", font=("Arial", 24), fg="white", bg="black")
        self.time_label.pack()

        self.time_entry = tk.Entry(self.window, font=("Arial", 24))
        self.time_entry.pack()

        self.start_button = tk.Button(self.window, text="开始倒计时", font=("Arial", 24), command=self.start_countdown)
        self.start_button.pack()

        self.countdown_label = tk.Label(self.window, text="", font=("Arial", 256), fg="white", bg="black")
        self.countdown_label.place(relx=0.5, rely=0.5, anchor="center")

        self.time_remaining = 0
        self.countdown_active = False
        self.countdown_id = None

        self.window.bind("<Escape>", self.exit_fullscreen)
        self.window.bind("<F7>", self.toggle_visibility)

    def start_countdown(self):
        if not self.countdown_active:
            time_input = self.time_entry.get()
            if time_input.isdigit():
                self.time_remaining = int(time_input)
                self.countdown_active = True
                self.update_countdown()

                # 隐藏输入框部分
                self.time_label.pack_forget()
                self.time_entry.pack_forget()
                self.start_button.pack_forget()

    def update_countdown(self):
        if self.time_remaining > 0 and self.countdown_active:
            self.countdown_label.config(text=str(self.time_remaining))
            self.time_remaining -= 1

            # 每秒钟播放提示音
            if self.time_remaining == 15:
                winsound.PlaySound("倒计时提示音(请更换).wav", winsound.SND_FILENAME)

            self.countdown_id = self.window.after(1000, self.update_countdown)
        else:
            self.countdown_active = False
            self.countdown_label.config(text="")
            self.window.update()

    def exit_fullscreen(self, event):
        self.window.attributes("-fullscreen", False)
        self.window.destroy()

    def toggle_visibility(self, event):
        # 切换输入框部分的可见性
        if self.time_label.winfo_ismapped():
            self.time_label.pack_forget()
            self.time_entry.pack_forget()
            self.start_button.pack_forget()
            if self.countdown_id:
                self.window.after_cancel(self.countdown_id)
                self.countdown_id = None
        else:
            self.time_label.pack()
            self.time_entry.pack()
            self.start_button.pack()

            # 重置倒计时相关变量
            self.countdown_active = False
            self.countdown_label.config(text="")
            self.time_remaining = 0


if __name__ == "__main__":
    countdown_gui = CountdownGUI()
    countdown_gui.window.mainloop()
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!