时钟工具v1.1-基于python

from tkinter import *
from ttkbootstrap.constants import *
from win32.win32api import GetSystemMetrics
from tkinter import colorchooser
import time,math
import tkinter as tk
from tkinter import ttk
import cv2
from ctypes import *
from datetime import datetime
def fliqlo():
    root=tk.Toplevel()
    root.title("翻页时钟")
    root.attributes('-fullscreen', True)
    root.focus()
    canvas = tk.Canvas(root, width=root.winfo_screenwidth(), height=root.winfo_screenheight())
    canvas.pack()

    def draw_rounded_square_with_text(x, y, w, h, radius, color, text):
        x0, y0 = x, y
        x1, y1 = x + w, y + h
        canvas.create_arc(x0, y0, x0 + radius * 2, y0 + radius * 2, start=90, extent=90, fill=color)
        canvas.create_arc(x1 - radius * 2, y0, x1, y0 + radius * 2, start=0, extent=90, fill=color)
        canvas.create_arc(x0, y1 - radius * 2, x0 + radius * 2, y1, start=180, extent=90, fill=color)
        canvas.create_arc(x1 - radius * 2, y1 - radius * 2, x1, y1, start=270, extent=90, fill=color)
        canvas.create_rectangle(x0 + radius, y0, x1 - radius, y1, fill=color)
        canvas.create_rectangle(x0, y0 + radius, x1, y1 - radius, fill=color)
        canvas.create_text((x0 + x1) // 2, (y0 + y1) // 2, text=text, fill="white", font=('DS-Digital',280))
    def exit_program(event):
        root.destroy()
    def update_clock():
        now = datetime.now()
        hour = now.strftime("%H")
        minute = now.strftime("%M")
        second = now.strftime("%S")
        canvas.delete("clock")  # 清除之前的时钟显示

        # 计算正方形的位置使其居中显示
        square_width = 400
        screenwidth=root.winfo_screenwidth()
        screenheight=root.winfo_screenheight()
        x_offset = (screenwidth-3*square_width)/4
        y_offset = (screenheight-square_width)/2
        draw_rounded_square_with_text(x_offset, y_offset, square_width, square_width, 50, "black", hour)
        draw_rounded_square_with_text(x_offset*2 + square_width, y_offset, square_width, square_width, 50, "black", minute)
        draw_rounded_square_with_text(x_offset*3 + square_width * 2, y_offset, square_width, square_width, 50, "black", second)
        root.after(1000, update_clock)  # 每隔1秒更新时钟
    update_clock()
    root.bind("<Key>", exit_program)
    root.mainloop()
    
def clock(hex_color,font=("微软雅黑", 250)):
    
    def update_clock():
        current_time = time.strftime("%H:%M:%S")
        clock_label.config(text=current_time)
        root.after(1000, update_clock)  # 每秒更新一次
    def exit_program(event):
        root.destroy()
    # 全屏显示
    root=tk.Toplevel()
    root.attributes("-fullscreen", True)
    root.focus()
    w=GetSystemMetrics(0)
    h=GetSystemMetrics(1)
    root.geometry(f'{w}x{h}')
    clock_label = tk.Label(root, font=font, fg=hex_color, bg="black", width=w,height=h)
    clock_label.pack(expand=True)
    update_clock()  
    root.bind("<Key>", exit_program)
    
def analog_clock():
    def update_clock():
        current_time = time.localtime()
        hours = current_time.tm_hour
        minutes = current_time.tm_min
        seconds = current_time.tm_sec
        
        hour_angle = (hours % 12 + minutes / 60.0) * 30
        minute_angle = minutes * 6
        second_angle = seconds * 6
        
        hour_hand_x = 150 + 50 * math.cos(math.radians(90 - hour_angle))
        hour_hand_y = 150 - 50 * math.sin(math.radians(90 - hour_angle))
        
        minute_hand_x = 150 + 70 * math.cos(math.radians(90 - minute_angle))
        minute_hand_y = 150 - 70 * math.sin(math.radians(90 - minute_angle))
        
        second_hand_x = 150 + 90 * math.cos(math.radians(90 - second_angle))
        second_hand_y = 150 - 90 * math.sin(math.radians(90 - second_angle))
        
        canvas.coords(hour_hand, 150, 150, hour_hand_x, hour_hand_y)
        canvas.coords(minute_hand, 150, 150, minute_hand_x, minute_hand_y)
        canvas.coords(second_hand, 150, 150, second_hand_x, second_hand_y)
        
        root.after(1000, update_clock)
    def exit_program(event):
        root.destroy()
    root=tk.Toplevel()
    root.title("Analog Clock")
    root.attributes('-fullscreen', True)
    root.focus()    
    canvas = tk.Canvas(root, width=300, height=300)
    canvas.pack(expand=True)

    clock_face = canvas.create_oval(50, 50, 250, 250, outline='black', width=2)
    hour_hand = canvas.create_line(150, 150, 150, 100, fill='black', width=3)
    minute_hand = canvas.create_line(150, 150, 150, 80, fill='black', width=2)
    second_hand = canvas.create_line(150, 150, 150, 70, fill='red', width=1)
    root.bind("<Key>", exit_program)
    update_clock()

    root.mainloop()
def col_clock(main):
    def choose_color():
        color = colorchooser.askcolor(title="选择颜色")
        if color[1]:
            hex_color = color[1]
            clock(hex_color)
  
    color_button = tk.Button(main,text="选择颜色", command=choose_color)
    color_button.pack(anchor='center',expand=True)






def left_lock():  
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
        # 调用摄像头摄像头
    cap = cv2.VideoCapture(0)
    #绝对路径
    # face_cascade = cv2.CascadeClassifier(rf'{cv2.data.haarcascades}\haarcascade_frontalface_default.xml')
    # eye_cascade = cv2.CascadeClassifier(rf'{cv2.data.haarcascades}\haarcascade_eye.xml')
    #相对路径
    i=0
    while(True): 
        # 获取摄像头拍摄到的画面
        ret, frame = cap.read()
        frame = cv2.flip(frame, 1)
        faces = face_cascade.detectMultiScale(frame, 1.3, 5)

        if  isinstance(faces,tuple):
            i=i+1
        else:
            i=0
        if i>=100:
            user32 = windll.LoadLibrary('user32.dll')
            user32.LockWorkStation()
            i=0    
        img = frame
        for (x,y,w,h) in faces:
            # 画出人脸框,蓝色,画笔宽度微
            img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            # 框选出人脸区域,在人脸区域而不是全图中进行人眼检测,节省计算资源
            face_area = img[y:y+h, x:x+w]
            eyes = eye_cascade.detectMultiScale(face_area)
            # 用人眼级联分类器引擎在人脸区域进行人眼识别,返回的eyes为眼睛坐标列表
            for (ex,ey,ew,eh) in eyes:
                #画出人眼框,绿色,画笔宽度为1
                cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)       
        # 实时展示效果画面
        # cv2.namedWindow('image',cv2.WINDOW_NORMAL)
        cv2.imshow('image',img)
        k=cv2.waitKey(1)
    # 点击窗口X按钮关闭窗口,窗口名字要对应
        if cv2.getWindowProperty('image',cv2.WND_PROP_VISIBLE) < 1.0: 
            break 
        if k == 27:
            break
        elif k == ord('s'):    
            cv2.imwrite('D:/project1/1.jpg', img)
    # 最后,关闭所有窗口
    cap.release()
    cv2.destroyAllWindows()



golbal_pid=None#机型:1表示非2023机型,0表示2023机型
font_all=('微软雅黑',10)
top_col='#863022'
left_col='#1B345D'
main_col='#9B7149'

def buttonchange(btn_list,cur_btn,label):
    for i in range(len(btn_list)):
        if i==cur_btn:
            btn_list[i].configure(bg=main_col,fg='black')
        else:
            btn_list[i].configure(bg=left_col)
    for child in label.winfo_children():
        child.destroy()      
def frame(btn,num):
    global golbal_pid
    golbal_pid=num
    
    if golbal_pid==0:
        for i,v in list(enumerate(btn)):
            if i<=4:
                v.place(relx=0, rely=0.08+(0.14*i), relwidth=0.08, relheight=0.14)
            else:
                v.place_forget()
    if golbal_pid==1:
        for i,v in list(enumerate(btn)):
            if i>4:
                v.place(relx=0, rely=0.08+0.14*(i-5), relwidth=0.08, relheight=0.14)
            else:
                v.place_forget()
# x_offset=100
# y_offset=100
# def start_move(event):
#     global x_offset
#     global y_offset
#     x_offset = event.x
#     y_offset = event.y

# def stop_move(event):
#     global x_offset
#     global y_offset
#     x_offset = None
#     y_offset = None

# def move_window(event,win):
#     x = event.x_root - x_offset
#     y = event.y_root - y_offset
#     win.geometry(f"+{x}+{y}") 


   
def main():
    
    win = tk.Tk()
    win.title("TOOL")
    # win.overrideredirect(True)#无边框
    # win.attributes('-alpha', 0.9)#窗口透明度
    style = ttk.Style()
    style.configure('Custom.TFrame')#圆角窗口带边框
    win.geometry('720x480+100+100')
    # win.wm_iconbitmap('./imgs/20-1.ico')
    # win.iconbitmap('./imgs/20-1.ico')
    # window.config()#设置背景颜色
    leftbox=tk.Label(win,text='M\nA\nX\nW\nA\nN\nG',fg='white',bg=left_col,font=('微软雅黑',14),borderwidth=0)
    leftbox.place(relx=0, rely=0, relwidth=0.08, relheight=1)
    topbox=tk.Label(win,text=' ',bg=top_col,font=('微软雅黑',14),borderwidth=1)
    topbox.place(relx=0, rely=0, relwidth=1, relheight=0.08)
    # topbox.bind("<ButtonPress-1>", start_move)
    # topbox.bind("<ButtonRelease-1>", stop_move)
    # topbox.bind("<B1-Motion>", lambda event,a=win: move_window(event,a))


    mainbox=tk.Label(win,text='待完善',bg=main_col,fg='white',font=font_all,cursor="arrow")
    mainbox.place(relx=0.08, rely=0.08, relwidth=0.92, relheight=1)
    
   
    #按钮
    pic0 = tk.PhotoImage(file='./imgs/0.png')
    btn0 = tk.Button(win, text='翻页时钟',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic0,activebackground=main_col,compound=TOP,command=lambda :fliqlo())
    pic1 = tk.PhotoImage(file='./imgs/1.png')
    btn1 = tk.Button(win, text='改色时钟',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic1,activebackground=main_col,compound=TOP,command=lambda :col_clock(mainbox))
    pic2 = tk.PhotoImage(file='./imgs/2.png')
    btn2 = tk.Button(win, text='数字时钟',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic2,activebackground=main_col,compound=TOP,command=lambda :clock('white',('DS-Digital',280)) )
    pic3 = tk.PhotoImage(file='./imgs/3.png')
    btn3 = tk.Button(win, text='指针时钟',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic3,activebackground=main_col,compound=TOP,command=lambda :analog_clock() )
    pic4 = tk.PhotoImage(file='./imgs/4.png')
    btn4 = tk.Button(win, text='pre',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic4,activebackground=main_col,compound=TOP,command=lambda :clock('white',('DS-Digital',280)))
    pic5 = tk.PhotoImage(file='./imgs/5.png')
    btn5 = tk.Button(win, text='_def',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic5,activebackground=main_col,compound=TOP,command=lambda :clock('white',('DS-Digital',280)))
    pic6 = tk.PhotoImage(file='./imgs/6.png')
    btn6 = tk.Button(win, text='_test1',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic6,activebackground=main_col,compound=TOP,command=lambda :clock('white',('DS-Digital',280)))
    pic7 = tk.PhotoImage(file='./imgs/7.png')
    btn7 = tk.Button(win, text='_test3',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic7,activebackground=main_col,compound=TOP,command=lambda :clock('white',('DS-Digital',280)))
    pic8 = tk.PhotoImage(file='./imgs/8.png')
    btn8 = tk.Button(win, text='_test4',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic8,activebackground=main_col,compound=TOP,command=lambda :clock('white',('DS-Digital',280)))
    pic9 = tk.PhotoImage(file='./imgs/9.png')
    btn9 = tk.Button(win, text='_pre',font=font_all,bg=left_col,fg='white',borderwidth=0,image=pic9,activebackground=main_col,compound=TOP,command=lambda :clock('white',('DS-Digital',280)))
    btn_list=[btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9]
    for i in range(len(btn_list)):
        btn_list[i].bind("<Button-1>", lambda event,index=i,label=mainbox: buttonchange(btn_list, index,label))
    
    #菜单栏
    pic = tk.PhotoImage(file='./imgs/start.png')
    main_menu = Menubutton (win,text=' 开始',font=('微软雅黑',12),relief=FLAT,borderwidth=0,fg='white',bg=top_col,image=pic,compound=LEFT)
    main_menu.place(relx=0, rely=0.009, relwidth=0.16, relheight=0.06)
    file_menu = Menu (main_menu, tearoff=False)
    file_menu.add_command (label="人走锁屏",font=font_all,command=lambda: left_lock())
    file_menu.add_command (label="功能列表1",font=font_all,command=lambda: frame(btn_list,0))
    file_menu.add_command (label="功能列表2",font=font_all,command=lambda: frame(btn_list,1))
    pic_ = tk.PhotoImage(file='./imgs/close.png')
    exit = Button (win,text=' 退出',font=('微软雅黑',12),fg='white',relief=FLAT,borderwidth=0,bg=top_col,image=pic_,compound=LEFT,command=lambda : win.destroy())
    exit.place(relx=0.16, rely=0.009, relwidth=0.16, relheight=0.06)
    win.bind("<Escape>", lambda event: win.destroy())
    main_menu.config(menu=file_menu)
    win.config (menu=main_menu)

    win.mainloop()
    
  
if __name__ == "__main__":
    main()




在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值