상세 컨텐츠

본문 제목

내컴퓨터 천천히 소리줄이며 원하시는 시간에 컴퓨터 종료하기

본문

반응형
import tkinter as tk
from tkinter import ttk
import datetime
import os
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
from comtypes import CLSCTX_ALL
from ctypes import cast, POINTER
import time

def set_volume(level):

    devices = AudioUtilities.GetSpeakers()
    interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
    volume = cast(interface, POINTER(IAudioEndpointVolume))
    volume.SetMasterVolumeLevelScalar(level, None)

def shutdown_computer():


    try:

        date_str = f"{year_var.get()}-{month_var.get()}-{day_var.get()} {hour_var.get()}:{minute_var.get()}"
        target_datetime = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M")
        now = datetime.datetime.now()
        delay = (target_datetime - now).total_seconds()

        if delay > 60:  # Only if more than 60 seconds remain
            # Schedule volume decrease gradually 1 minute before shutdown
            root.after(int(delay - 60) * 1000, gradual_volume_decrease)
            # Schedule shutdown
            root.after(int(delay) * 1000, lambda: os.system("shutdown /s /t 1"))
            countdown(int(delay))
        else:

            time_label.config(text="스케줄을 잡을 시간이 부족해요!")


    except Exception as e:
        time_label.config(text=str(e))

def gradual_volume_decrease():

    devices = AudioUtilities.GetSpeakers()
    interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
    volume = cast(interface, POINTER(IAudioEndpointVolume))
     
    current_volume = volume.GetMasterVolumeLevelScalar()

    while current_volume > 0:
        new_volume = max(0, current_volume - 0.03)  # 0 이하로 내려가지 않게 합니다
        volume.SetMasterVolumeLevelScalar(new_volume, None)
        current_volume = new_volume
        print(f"볼륨 설정: {current_volume * 100}%")
       
        time.sleep(1)  # 10초 대기


def countdown(t):

    def update():
        nonlocal t
        mins, secs = divmod(t, 60)
        hours, mins = divmod(mins, 60)
        timeformat = '{:02d}:{:02d}:{:02d}'.format(hours, mins, secs)
        time_label.config(text=timeformat)
        t -= 1
        if t >= 0:
            root.after(1000, update)
        else:
            time_label.config(text="종료 시간이 다 됐습니다.")
    update()

root = tk.Tk()
root.title("컴퓨터 자동종료")
root.geometry("480x200")


# Year, Month, Day, Hour, Minute Dropdowns with today's date preselected
now = datetime.datetime.now()

year_var = tk.StringVar(value=now.year)
month_var = tk.StringVar(value=f"{now.month:02d}")
day_var = tk.StringVar(value=f"{now.day:02d}")
hour_var = tk.StringVar(value=f"{now.hour:02d}")
minute_var = tk.StringVar(value=f"{now.minute:02d}")

years = [str(year) for year in range(now.year, now.year + 2)]
months = [f"{m:02d}" for m in range(1, 13)]
days = [f"{d:02d}" for d in range(1, 32)]
hours = [f"{h:02d}" for h in range(24)]
minutes = [f"{m:02d}" for m in range(60)]

# GUI Components
 
ttk.Label(root, text="설정시간에 PC 종료되며, 종료 1분전에 컴퓨터 소리를 점차 줄입니다",font=('NanumGothic', 10)).grid(row=0, column=0,columnspan=6,padx=10, pady=20)
ttk.Label(root, text="", width=2,font=('NanumGothic', 15, 'bold')).grid(row=1, column=0, padx=0, pady=0)
ttk.Combobox(root, textvariable=year_var, values=years, width=5,font=('NanumGothic', 15, 'bold')).grid(row=1, column=1, padx=0, pady=0)
ttk.Combobox(root, textvariable=month_var, values=months, width=3,font=('NanumGothic', 15, 'bold')).grid(row=1, column=2, padx=0, pady=0)
ttk.Combobox(root, textvariable=day_var, values=days, width=3,font=('NanumGothic', 15, 'bold')).grid(row=1, column=3, padx=0, pady=0)
ttk.Combobox(root, textvariable=hour_var, values=hours, width=3,font=('NanumGothic', 15, 'bold')).grid(row=1, column=4, padx=0, pady=0)
ttk.Combobox(root, textvariable=minute_var, values=minutes, width=3,font=('NanumGothic', 15, 'bold')).grid(row=1, column=5, padx=0, pady=0)

time_label = tk.Label(root, text="", font=('NanumGothic', 15, 'bold'),fg="red")
time_label.grid(row=2, column=0, columnspan=6, padx=5, pady=5)

shutdown_button = tk.Button(root, text="종료 예약하기", font=('NanumGothic', 13), command=shutdown_computer)
shutdown_button.grid(row=3, column=2, padx=5, pady=5)

cancel_button = tk.Button(root, text="종료 취소", font=('NanumGothic', 13),command=root.destroy)
cancel_button.grid(row=3, column=4, padx=5, pady=5)

root.mainloop()

사용자가 맞추어 놓은 컴퓨터에서 종료시간 1분전에 컴퓨터 스피커 소리를 점차 줄어들게 하여, 음소거 된후

컴퓨터가 종료되어 집니다.

반응형

관련글 더보기