倒计时、抽号器、计时器、python、图形化界面

不说了,直接上代码!!!!!

from tkinter import * 
from tkinter import ttk
import time
import random
import threading
import tkinter.simpledialog
import tkinter.font as tkFont

class times_tools:
    def __init__(self,root):
        
        self.my_font = tkFont.Font(family='宋体', size=52)
        self.font_all=tkFont.Font(family='微软雅黑', size=10)
        self.end_font=tkFont.Font(family='楷体', size=8)

        self.frame1=Frame(root,bg="#F0FFFF")
        self.frame1.pack( expand=True, fill="both")
        self.label1=Label(self.frame1,text="",bg="#FFFFFF",fg="black",font=self.my_font,width=8,height=1)
        self.label1.pack( expand=True, fill="both")
        
        self.frame_separ1=Frame(root,bg="#F0FFFF")
        self.frame_separ1.pack(fill="x",pady=1)
        self.separ1=self.separ(self.frame_separ1)

        self.frame_2=Frame(root,bg="#F0FFFF")
        self.frame_2.pack(expand=True, fill="both",padx=0,pady=0)
        frame_2_1=Frame(self.frame_2,bg="#F0FFFF")
        frame_2_1.pack(expand=True, fill="both",side='left',padx=2,pady=1)
        frame_2_2=Frame(self.frame_2,bg="#F0FFFF")
        frame_2_2.pack(expand=True, fill="both",side='left',padx=0,pady=0)
        self.label2=Label(frame_2_1,text="计时/秒数:",fg="black",font=self.font_all,\
                bd=0,bg="#F0FFFF",width=12)
        self.label2.pack(expand=True,fill=BOTH,side='left',)
        self.time_max_num=Entry(frame_2_1,width=8,bd=1,relief=FLAT,justify=CENTER,bg="#FFFFFF",font=self.font_all,)
        self.time_max_num.pack(expand=True,fill=BOTH,side='left')
        self.time_max_num.config(validate='key', validatecommand=(root.register(self.is_integer), '%P'))
        self.time_max_num.insert(0,'60')
        self.count_down_button=Button(frame_2_2,text="开始/暂停",fg="#F0FFFF",font=self.font_all,bg="#87CEEB",\
            command=self.count_down_but, pady=0,width=10,padx=0,\
            activebackground="#87CEEB",activeforeground="#F0FFFF",bd=0)
        self.count_down_button.pack(expand=True,fill=BOTH,side='left',padx=0)
        
        self.frame_separ2=Frame(root,bg="#F0FFFF")
        self.frame_separ2.pack(fill="x",pady=1)
        self.separ2=self.separ(self.frame_separ2)

        frame_3=Frame(root,bg="#F0FFFF")
        frame_3.pack( expand=True, fill="both",pady=0)
        frame_3_1=Frame(frame_3,bg="#F0FFFF")
        frame_3_1.pack(expand=True,fill=BOTH,side='left')
        frame_3_1_1=Frame(frame_3_1,bg="#F0FFFF")
        frame_3_1_1.pack(pady=0,padx=0,expand=True,fill=BOTH)
        frame_3_1_1_1=Frame(frame_3_1_1,bg="#F0FFFF")
        frame_3_1_1_1.pack(pady=0,padx=1,expand=True,fill=BOTH)
        self.label3=Label(frame_3_1_1_1,text="抽号/号数:",fg="black",font=self.font_all,\
                bd=0,bg="#F0FFFF",justify="right",width=12) 
        self.label3.pack(expand=True,fill=BOTH,side=LEFT,pady=1)
        self.draw_max_num=Entry(frame_3_1_1_1,width=8,bd=1,relief=FLAT,justify=CENTER,bg="#FFFFFF",font=self.font_all)
        self.draw_max_num.config(validate='key', validatecommand=(root.register(self.is_integer), '%P'))
        self.draw_max_num.pack(expand=True,fill=BOTH,side=LEFT,pady=1)
        self.draw_max_num.insert(0,'60')
        frame_3_1_1_2=Frame(frame_3_1_1,bg="#F0FFFF")
        frame_3_1_1_2.pack(pady=0,padx=1,expand=True,fill=BOTH)
        self.label4=Label(frame_3_1_1_2,text="过滤/号数:",fg="black",font=self.font_all,\
                bd=0,bg="#F0FFFF",width=12)
        self.label4.pack(expand=True,fill=BOTH,side=LEFT,pady=1,padx=1)
        
        self.filter_add_del_num=Entry(frame_3_1_1_2,width=8,bd=1,relief=FLAT,justify=CENTER,bg="#FFFFFF",\
                font=self.font_all)
        self.filter_add_del_num.pack(expand=True,fill=BOTH,side=LEFT,pady=1,padx=1)
        self.filter_add_del_num.config(validate='key', validatecommand=(root.register(self.is_int_filter), '%P'))
        frame_3_1_1_3=Frame(frame_3_1_1,bg="#F0FFFF")
        frame_3_1_1_3.pack(pady=0,padx=1,expand=True,fill=BOTH)
        self.filter_num_rang=Entry(frame_3_1_1_3,bd=1,relief=FLAT,justify=LEFT,bg="#FFFFFF",font=self.font_all)
        self.filter_num_rang.pack(expand=True,fill=BOTH,pady=0,padx=1)
        self.filter_num_rang.insert(0,"过滤已开启!")
        self.filter_num_rang["state"]=DISABLED

        frame_3_2=Frame(frame_3,bg="#F0FFFF")
        frame_3_2.pack(expand=True,fill=BOTH,side='left',pady=0)
        self.draw_num_button=Button(frame_3_2,text="开始/暂停",fg="#F0FFFF",font=self.font_all,bg="#87CEEB",\
            command=self.draw_num_but,width=10,pady=0,padx=0,\
            activebackground="#87CEEB",activeforeground="#F0FFFF",bd=0)
        self.draw_num_button.pack(expand=True,fill=BOTH,pady=1,padx=0)
        self.filter_num_add_del_button=Button(frame_3_2,text="增加/删除",fg="#F0FFFF",font=self.font_all,bg="#87CEEB",\
            command=self.filter_num_add_del_num_but,width=10,\
            activebackground="#87CEEB",activeforeground="#F0FFFF",bd=0)
        self.filter_num_add_del_button.pack(expand=True,fill=BOTH,pady=1,padx=0)
        self.filter_num_re_button=Button(frame_3_2,text="重置",fg="#F0FFFF",font=self.font_all,bg="#87CEEB",\
            padx=0,pady=0,command=self.filter_num_re_but,justify="right",\
            activebackground="#87CEEB",activeforeground="#F0FFFF",bd=0,width=10)
        self.filter_num_re_button.pack(expand=True,fill=BOTH,pady=1,padx=0)
        self.filter_num_re_button["state"]=ACTIVE
        
        self.frame_separ3=Frame(root,bg="#F0FFFF")
        self.frame_separ3.pack(fill="x",pady=1)
        self.separ3=self.separ(self.frame_separ3)

        frame_end_row=Frame(root,bg="#F0FFFF")
        frame_end_row.pack(fill=BOTH)
        self.version_var=BooleanVar(value=False)
        self.version_ch=Checkbutton(frame_end_row,text="V2",font=self.end_font,command=self.version_check,bd=0,bg="#F0FFFF",variable=self.version_var)
        self.version_ch.pack(side='right',pady=0,padx=0)
        self.filter_lock_num= BooleanVar(value=False)
        self.filter_lock_num_check=Checkbutton(frame_end_row,text="锁定",font=self.end_font,variable=self.filter_lock_num,bg="#F0FFFF")
        self.filter_lock_num_check.pack(side='right',pady=0,padx=0)
        self.filter_memory_lock= BooleanVar(value=False)
        self.filter_memory_check=Checkbutton(frame_end_row,text="记录",font=self.end_font,variable=self.filter_memory_lock,bg="#F0FFFF")
        self.filter_memory_check.pack(side='right',pady=0,padx=0)
        self.filter_lock= BooleanVar(value=True)
        self.filter_check=Checkbutton(frame_end_row,text="过滤",font=self.end_font,variable=self.filter_lock,command=self.filter_ch,bg="#F0FFFF")
        self.filter_check.pack(fill=BOTH,side='right',pady=0,padx=0)
        self.interface_lock = BooleanVar(value=False)
        self.interface_lock_check=Checkbutton(frame_end_row,text="固定",font=self.end_font,variable=self.interface_lock,command=self.interface_lock_th,bg="#F0FFFF")
        self.interface_lock_check.pack(side='right',pady=0,padx=0)
        self.full_screen_lock=BooleanVar(value=False)
        self.full_screen_check=Checkbutton(frame_end_row,text="全屏",font=self.end_font,variable=self.full_screen_lock,command=self.full_screen_ch,bg="#F0FFFF")
        self.full_screen_check.pack(fill=BOTH,side='right',pady=0,padx=0)
        self.topmost_var = BooleanVar(value=True)
        self.topmost_check=Checkbutton(frame_end_row,text="置顶",font=self.end_font,variable=self.topmost_var,command=self.topmost_ch,bg="#F0FFFF")
        self.topmost_check.pack(fill=BOTH,side='right',pady=0,padx=0)
        
        #抽号相关变量 #抽号线程
        self.draw_arr=[]
        self.draw_lock=0
        self.draw_lock1=1
        self.filter_arr=[]
        self.max_num=0
        self.draw_event=self.thread(self.draw_num)

        #倒计时相关变量 #倒计时线程
        self.count_down_lock=0
        self.count_down_event=self.thread(self.count_down)
        self.count_down_lock1=0
        self.count_down_max_num=0
        
        root.bind("<Configure>",self.font_size)

    def is_integer(self,text):
        try:
            if text=="":
                return True
            else:
                text=int(text)
                return True
        except :
            return False
        
    def is_int_filter(self,text):
        arr1=text.split(",")
        for i in range(len(arr1)):
            if self.is_integer(arr1[i]):
                lock=1
            else:
                arr2=arr1[i].split("-")
                for k in range(len(arr2)):
                    if self.is_integer(arr2[k]):
                        lock=1
                    else:
                        lock=0
                        break
        if lock == 1 :
            return True
        else:
            return False
    #水平线
    def separ(self,root):
        separator=ttk.Separator(root, orient="horizontal")
        separator.pack(side='bottom',fill='x',padx=0,pady=0,anchor='w')
        return separator
    #字体
    def font_size(self,event):
        root_width = root.winfo_width()
        root_height = root.winfo_height()
        self.new_font_size = max(52, int(root_width / 6),int(root_height / 5))
        self.my_font.configure(size=self.new_font_size)
        self.font_size_all = max(10, int(root_height / 24),int(root_width / 45))
        self.font_all.configure(size=self.font_size_all)
    #线程
    def thread(self,func):
        event=threading.Event()
        thread=threading.Thread(target=func,args=(event,),)
        thread.setDaemon(True)
        thread.start()
        return event
    #开关
    def lock(self,lock):
        if lock == 1 :
            return 0
        else :
            return 1
    #验证数字类型返回0或1
    def return_lock(self,str):
        try:
            str=int(str)
            lock=1
        except:
            lock=0
        return lock
    #抽号范围
    def draw_num_rang(self):
        self.draw_arr.clear()
        num=self.draw_max_num.get()
        num=int(num)
        self.draw_lock1=1
        if self.max_num == 0 :
            self.max_num=num
        elif self.max_num != num:
            self.filter_arr.clear()
            self.max_num=num
        if self.draw_lock1 == 1:
            for i in range(num):
                self.draw_arr.append(i+1)
        if self.filter_lock.get():
            self.draw_filter_num()
    #过滤已抽号数
    def draw_filter_num(self):
        if len(self.filter_arr) == int(self.draw_max_num.get()):
            self.filter_arr.clear()
            self.filter_text()
        if len(self.filter_arr) > 0 :
            for i in range(len(self.filter_arr)) :
                self.draw_arr.remove(self.filter_arr[i])     
    #过滤号数显示
    def filter_text(self):
        text=""
        if len(self.filter_arr) > 0 :
            for i in range (len(self.filter_arr)):
                text=text+str(self.filter_arr[i])+","
        text="已过滤:"+text
        self.filter_num_rang["state"]=NORMAL
        self.filter_num_rang.delete(0,END)
        self.filter_num_rang.insert(0,text)
        self.filter_num_rang["state"]=DISABLED
    #开始抽号函数
    def draw_num(self,event):
        while True :
            self.draw_num_rang()
            event.wait()
            if len(self.draw_arr) != 0 :
                self.label1["text"]=random.choice(self.draw_arr)
            root.update()
    #结束抽号
    def draw_stop(self):
        self.draw_event.clear()
        if self.filter_lock.get() and self.filter_lock_num.get()==False:
            try : 
                self.filter_arr.append(int(self.label1["text"]))
            except :
                pass
            self.filter_text()
    #倒计时默认配置
    def count_down_value(self):
        self.count_down_lock=0
        self.count_down_lock1=0
        self.count_down_event.clear()
    #倒计时函数
    def count_down(self,event):
        while True :
            max_time=int(self.time_max_num.get())
            finish_lock=1
            for i in range(max_time,0,-1):
                event.wait()
                if self.count_down_lock1 == 0 :
                    finish_lock=0
                    self.count_down_lock1=1
                    break
                self.label1["text"]=i
                time.sleep(1)
            if finish_lock == 1 :
                self.label1["text"]="计时结束"
                self.count_down_value()
    
    def count_down_but(self):
        if self.draw_lock == 1 :
            self.draw_lock=0
            self.count_down_lock1=0
            self.draw_event.clear()
        if self.time_max_num.get() == "" :
            self.time_max_num.insert(0,"0")
        if self.count_down_max_num == 0 :
            self.count_down_max_num = int(self.time_max_num.get())
            self.count_down_lock = self.lock(self.count_down_lock)
        elif self.count_down_max_num != int(self.time_max_num.get()):
            self.count_down_value()
            self.count_down_lock = 1
            self.count_down_max_num = int(self.time_max_num.get())
        elif self.count_down_max_num == int(self.time_max_num.get()):
            self.count_down_lock = self.lock(self.count_down_lock)
        else:
            self.count_down_lock = self.lock(self.count_down_lock)
        if self.count_down_lock == 1 :
            self.count_down_event.set()
        else:
            self.count_down_event.clear()
            self.count_down_lock1=0
            self.label1["text"]="计时暂停"
    
    def filter_stop(self):
        self.filter_num_rang["state"]=NORMAL
        self.filter_num_rang.delete(0,END)
        self.filter_num_rang.insert(0,"过滤已关闭!")
        self.filter_num_rang["state"]=DISABLED
    
    def filter_start(self):
        self.filter_num_rang["state"]=NORMAL
        self.filter_num_rang.delete(0,END)
        self.filter_num_rang.insert(0,"过滤已开启!")
        self.filter_num_rang["state"]=DISABLED
        
    def draw_num_but(self):
        if self.count_down_lock == 1 :
            self.count_down_value()
        self.lock1=0
        self.draw_lock=self.lock(self.draw_lock)
        if self.draw_lock == 1 :
            self.draw_event.set()
        else:
            self.draw_stop()
            if self.topmost_var.get()==False and self.version_var.get():
                try :
                    num = int(self.version_ch["text"])
                    self.label1["text"]=num
                except :
                    pass
               
    def filter_num_add_del_num_but(self):
        arr1=self.filter_add_del_num.get().split(",")
        filter_rang=[]
        if_success=1
        for i in range(len(arr1)):
            if self.is_integer(arr1[i]):
                filter_rang.append(arr1[i])
            else:
                arr2=arr1[i].split("-")
                if len(arr2) == 2 :
                    min = int(arr2[0])   
                    max = int(arr2[1]) 
                    if min < max :
                        for k in range(min,max+1):
                            filter_rang.append(k)
                    else:
                        if_success=0
        for i in range(len(filter_rang)):
            filter_lock=self.return_lock(filter_rang[i])
            if filter_lock == 1 :
                max_num=int(self.draw_max_num.get())
                num=int(filter_rang[i])
                if num <= max_num:
                    lock=1
                    if len(self.filter_arr) > 0 :
                        for i in range(len(self.filter_arr)):
                            if self.filter_arr[i] == num:
                                self.filter_arr.remove(num)
                                self.filter_text()
                                lock=0
                                break
                    if lock == 1 :
                        self.filter_arr.append(num)
                        self.filter_text()
                elif num > max_num :
                    if_success=0
        if if_success == 1 :
            self.label1["text"]="操作成功"
        else:
            self.label1["text"]="部分失败"
        
    def filter_num_re_but(self):
        self.filter_arr.clear()
        self.draw_num_rang()
        self.filter_text()

    def version_check(self):
        if self.topmost_var.get():
            self.version_ch["text"]="V2"
        else:
            if self.version_var.get():
                version_con = tkinter.simpledialog.askstring("指定号数", "强制抽中号数:",initialvalue="V2")
                self.version_ch["text"]=version_con
                self.filter_lock.set(False)
                self.filter_memory_lock.set(False)
                self.filter_ch()
            else:
                self.version_ch["text"]="V2"

    def topmost_ch(self):
        if self.topmost_var.get():
            root.attributes('-topmost', True)
            self.version_ch["text"]="V2"
        else:
            root.attributes('-topmost', False)

    def filter_ch(self):
        if self.filter_lock.get():
            if self.filter_memory_lock.get():
                self.version_ch["text"]="V2"
                if len(self.filter_arr) == 0 :
                    self.filter_start()
            else:
                self.filter_start()
                self.version_ch["text"]="V2"
        else:
            if self.filter_memory_lock.get():
                if len(self.filter_arr) == 0 :
                    self.filter_stop()
            else:    
                self.filter_arr.clear()
                self.filter_stop()

    def interface_lock_th(self):
        if self.interface_lock.get():
            root.resizable(False,False)
            self.full_screen_lock.set(False)
            self.full_screen_ch()
            root.update()
        else:
            root.resizable(True,True)
            root.update()

    def full_screen_ch(self):
        if self.full_screen_lock.get():
            root.attributes('-fullscreen', True)
        else:
            root.attributes('-fullscreen', False)

if __name__=="__main__" :
    root=Tk()
    root.title("倒计时&&抽号工具")
    root.configure(bg="#F0FFFF")
    root.attributes('-topmost', True)
    #root.iconbitmap('A.ico')
    #canvas = tk.Canvas(root, bg='white')
    #canvas.pack(fill='both', expand=True)
    #frame = tk.Frame(canvas, bg='white')
    #canvas.create_window(0, 0, window=frame, anchor='nw')
    #frame.pack(fill=BOTH,expand=True,side=LEFT)
    app=times_tools(root)
    root.geometry('350x280')
    root.mainloop()
    
    

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值