windows 倒计时,使用python tkinter实现

helei 2023-4-4 608 4/4
#!/usr/bin/python3
from tkinter import *
import base64
import time
import _thread
import queue
import pygame
class Application(Frame):
    """右键菜单item点击事件

       点击后开启多线程启动倒计时
       Args:
            self:
            time:倒计时时间
       Returns:None
    """
    def say_hi(self,time):
        if pygame.mixer.music.get_busy():
            pygame.mixer.music.stop()
        self.threadstop = False
        timetext = ""
        timemax = time
        if timemax%60<10:
            if timemax/60<10:
                timetext = "0"+str(int(timemax/60))+":0"+str(timemax%60)
            else:
                timetext = str(int(timemax/60))+":0"+str(timemax%60)
        else:
            if timemax/60<10:
                timetext = "0"+str(int(timemax/60))+":"+str(timemax%60)
            else:
                timetext = str(int(timemax/60))+":"+str(timemax%60)
        self.timeon.set(timetext)
        self.timenum.set(time)
        #self.var.set(120)#设置变量
        #self.label.configure(font=('Arial',self.var.get()))#重新设置label配置font参数
        #重新配置label的颜色
        self.label.config(fg="#000000")
        #如果有线程在执行,则退出线程,执行下一个
        if  self.threadstart:
            self.threadstop = True
        #while循环,保证上个线程内的循环一定结束了
        while  self.threadstart:
            print("在执行")
            continue
        print("开新线程")
        # 开启多线程执行倒计时
        _thread.start_new_thread(self.duetime,("Thread-1", 0, ))
    """创建控件
       Args:
            self:
       Returns:None
    """
    def createWidgets(self):
        #创建一个label用于显示文本,textvariable用于根据设置的变量功态改变文本内容
        self.label = Label(self,bd=20,width=300,height=150,font=('Arial', 50),pady=10,textvariable=self.timeon)
        self.label.pack()
        #创建一个button
        #self.btn = Button(self,text="变大",command=self.say_hi)
        #self.btn.pack()
        #创建右键菜单
        self.menu = Menu(self,tearoff=False)#tearoff=False关闭顶部线条
        # self.menu_item = Menu(self.menu,tearoff=False)
        #添加子菜单
        # for item in ["子菜单1", "子菜单2", "子菜单3", "子菜单4", "子菜单5"]:
        # self.menu_item.add_command(label=item)
        #self.menu.add_cascade(label="总菜单",menu=self.menu_item)
        #添加菜单,使用 command=lambda:fuc(args)来给方法传参    
        self.menu.add_command(label="开始00:03倒计时", command=lambda:self.say_hi(3))        
        self.menu.add_command(label="开始1:00倒计时", command=lambda:self.say_hi(60))
        # self.menu.add_separator()#添加分割线
        self.menu.add_command(label="开始1:30倒计时", command=lambda:self.say_hi(90))
        self.menu.add_command(label="开始2:00倒计时", command=lambda:self.say_hi(120))
        self.menu.add_command(label="开始2:30倒计时", command=lambda:self.say_hi(150))
        # self.menu.add_separator()
        self.menu.add_command(label="开始3:00倒计时", command=lambda:self.say_hi(180))
        self.menu.add_command(label="开始4:00倒计时", command=lambda:self.say_hi(240))
        self.menu.add_command(label="开始5:00倒计时", command=lambda:self.say_hi(300))
        self.menu.add_command(label="开始6:00倒计时", command=lambda:self.say_hi(360))
        self.menu.add_command(label="开始7:00倒计时", command=lambda:self.say_hi(420))
        self.menu.add_command(label="开始8:00倒计时", command=lambda:self.say_hi(480))
        self.menu.add_command(label="开始9:00倒计时", command=lambda:self.say_hi(540))
        self.menu.add_command(label="开始10:00倒计时", command=lambda:self.say_hi(600))
        self.menu.add_command(label="开始15:00倒计时", command=lambda:self.say_hi(900))
        self.menu.add_command(label="开始20:00倒计时", command=lambda:self.say_hi(1200))
        self.menu.add_command(label="开始25:00倒计时", command=lambda:self.say_hi(1500))
        self.menu.add_command(label="开始30:00倒计时", command=lambda:self.say_hi(1800))
        self.menu.add_command(label="开始60:00倒计时", command=lambda:self.say_hi(3600))
        self.menu.add_command(label="退出", command=self.master.quit)
    """倒计时,改变label文本

       监听窗口变化,动态改变label文本
       Args:
            self:
            threadName:
            delay:
       Returns:None
    """
    def duetime(self,delay,name):
        self.threadstart = True
        timemax = self.timenum.get()
        print(timemax)
        while timemax>0 and not self.threadstop:
            time.sleep(1)
            timemax = timemax -1
            if timemax%60<10:
                if timemax/60<10:
                    timetext = "0"+str(int(timemax/60))+":0"+str(timemax%60)
                else:
                    timetext = str(int(timemax/60))+":0"+str(timemax%60)
            else:
                if timemax/60<10:
                    timetext = "0"+str(int(timemax/60))+":"+str(timemax%60)
                else:
                    timetext = str(int(timemax/60))+":"+str(timemax%60)
            print("设置文本时间:"+timetext)
            self.timeon.set(timetext)
        redtext = 0
        while timemax<1 and timemax>-3600 and not self.threadstop:
            self.threadstart = True
            #重新配置label的颜色
            self.label.config(fg="red")
            timemax = timemax -1
            time.sleep(1)
            if self.threadstop:
                self.threadstart = False
                self.threadstop = False
                break
            redtext = redtext+1
            if redtext%60<10:
                if redtext/60<10:
                    timetext = "0"+str(int(redtext/60))+":0"+str(redtext%60)
                else:
                    timetext = str(int(redtext/60))+":0"+str(redtext%60)
            else:
                if redtext/60<10:
                    timetext = "0"+str(int(redtext/60))+":"+str(redtext%60)
                else:
                    timetext = str(int(redtext/60))+":"+str(redtext%60)
            print("设置文本时间2:"+timetext)
            self.timeon.set(timetext)
            #超倒计时五分钟播放音频
            if timemax == -300:
                #初始化音频播放
                pygame.mixer.music.load("C:/2228.mp3")
                pygame.mixer.music.play()
        print("我结束了")
        self.threadstart = False
        self.threadstop = False
    """打开右键菜单
       Args:
            self:
            event
       Returns:None
    """
    def xShowMenu(self,event):
        print("点击右键了")
        self.menu.post(event.x_root, event.y_root)#设置右键菜单的初始显示坐标,为鼠标坐标
    """窗口变化事件
       监听窗口变化,动态改变label文本大小
       Args:
            self:
            event
       Returns:None
    """
    def window_resize(self,event):
        print("窗口变化了")
        self.update()
        print(event)
        if event.width>200:
            scale_x = event.width/200
            scale_y = event.height/150
            font_size = 60
            if scale_x > scale_y:
                print(scale_x)
                font_size = font_size*scale_y
            else:
                print(scale_y)
                font_size = font_size*scale_x
            print(font_size)
            self.var.set(font_size)
            self.label.configure(font=('Arial',self.var.get()))
        elif event.width<200:
            scale_x = event.width/200
            scale_y = event.height/150
            font_size = 60
            if scale_x > scale_y:
                print(scale_x)
                font_size = font_size*scale_y
            else:
                print(scale_y)
                font_size = font_size*scale_x
            print(font_size)
            self.var.set(font_size)
            self.label.configure(font=('Arial',self.var.get()))
        else :
            self.var.set(60)
            self.label.configure(font=('Arial', self.var.get()))
    """鼠标左键按下事件
        监听鼠标左键点击,设置窗口当前位置和鼠标当前位置
    """
    def StartMove(self, event):
        print("开始拖动")
        print(event)
        self.x = event.x
        self.y = event.y
        self.win_point_x = self.master.winfo_x()
        self.win_point_y = self.master.winfo_y()
    """鼠标左键松开事件
        松开鼠标左键设置窗口位置
    """
    def StopMove(self, event):
        print("停止拖动")
        print(event)
        self.master.geometry("+%s+%s" % (event.x-self.x+self.win_point_x, event.y-self.y+self.win_point_y))
        self.x = None
        self.y = None
    """ 鼠标左键拖动事件
    """
    def OnMotion(self, event):
        self.master.geometry("+%s+%s" % (event.x-self.x+self.win_point_x, event.y-self.y+self.win_point_y))
    def __init__(self, master=None):
        Frame.__init__(self, master,width="200",height="100")
        self.var=IntVar()#定义一个字符串变量
        self.var.set(60)
        self.timeon = StringVar()#定义倒计时文本
        self.timeon.set("00:00")#定义倒计时文本
        self.timenum = IntVar()#定义倒计时文本
        self.timenum.set(0)#定义倒计时文本
        master.bind('<Configure>', self.window_resize)#监听窗口变化事件
        master.bind("<ButtonPress-1>", self.StartMove)#鼠标左键按下
        master.bind("<ButtonRelease-1>", self.StopMove)#鼠标左键松开
        master.bind("<B1-Motion>", self.OnMotion)#鼠标左键移动
        master.bind("<Button-3>", self.xShowMenu)#监听鼠标右键事件
        self.master = master
        self.pack()
        self.createWidgets()
        self.threadstart = False#判断是否有线程已经执行
        self.threadstop  = False#定义退出线程循环变量
        #开始倒计时
        #self.duetime()#开始倒计时
        pygame.mixer.init()
root = Tk()
root.wm_attributes('-topmost',1)#窗口置顶
root.overrideredirect(1)#隐藏标题栏
# root.wm_attributes('-fullscreen','true')
root.title("运总倒计时工具")
root.geometry("200x150+0+0")
# root.iconbitmap("H:\work\yz\img\logo.ico")
app = Application(master=root)
app.mainloop()
- THE END -

helei

4月19日17:24

最后修改:2023年4月19日
0

非特殊说明,本博所有文章均为博主原创。