分享一个 gui全屏倒计时代码 | python代码速记 | python 技术论坛-金年会app官方网

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("", self.exit_fullscreen)
        self.window.bind("", 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
(= ̄ω ̄=)··· 暂无内容!

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