简单的python代码,使用tkinter库实现交互,定时关机,适用于windows系统。
import os
import sys
import time
import tkinter as tk
from threading import Thread
from tkinter import ttk
def get_time():#获取当前的时间
h=time.strftime("%H", time.localtime())
m=time.strftime("%M", time.localtime())
if len(h)==1:
h="0"+h
if len(m)==1:
h="0"+h
return (h,m)
def close_threading(hour,min):#监测时间并且关机电脑的子线程
global thread_running
bool=True
while bool and thread_running:
time=get_time()
if (time[0]==hour and time[1]==min):
bool=False
if not(bool):
os.system("shutdown -s -t 0")#cmd关机命令
threading=True#控制时间检测线程
def window2(hour,min,root=False):
def open_window1(root):
global thread_running
thread_running=False
window1(root)
if root!=False:
for i in root.winfo_children():
i.destroy()
else:
root = tk.Tk()
global thread_running
thread_running=True
thread1 = Thread(target=close_threading, args=(hour,min))
thread1.start()
root.title("定时关机工具")
ttk.Label(root, text="将在"+hour+":"+min+"自动关机",font=("微软雅黑", 14)).place(x=50,y=30,)
ttk.Label(root, text="正在等待中....请勿关闭窗口",font=("微软雅黑", 10)).place(x=50,y=80,)
ttk.Label(root, text="点击取消取消关机",font=("微软雅黑", 14)).place(x=50,y=130,)
ttk.Label(root, text="made by lhc",font=("微软雅黑", 7)).place(x=150,y=200,)
confirm_btn = ttk.Button(root, text="取消", command=lambda :window1(root),width=20)
confirm_btn.grid(row=2, column=0, columnspan=2, pady=10)
confirm_btn.place(x=50,y=250)
# 设置窗口初始大小
# 运行主循环
root.mainloop()
thread_running =False
def window1(root=False):
def on_confirm(root):
# 获取选择的时间并处理
window2(hour_combobox.get(),minute_combobox.get(),root)
# 这里可以添加其他逻辑(如关闭窗口、存储时间等)
if root!=False:
for i in root.winfo_children():
i.destroy()
else:
root = tk.Tk()
# 创建主窗口
root.title("定时关机工具")
root.geometry("250x300")
root.resizable(0, 0)
# 生成时间选项(两位数格式)
hours = [f"{i:02d}" for i in range(24)]
minutes = [f"{i:02d}" for i in range(60)]
ttk.Label(root, text="选择关机时间:",font=("微软雅黑", 14)).place(x=70,y=30,)
# 创建组件
ttk.Label(root, text="小时:").place(x=50,y=100)
hour_combobox = ttk.Combobox(root, values=hours, width=5)
hour_combobox.place(x=100,y=100)
hour_combobox.set("00") # 设置默认值为00
ttk.Label(root, text="分钟:").place(x=50,y=150)
minute_combobox = ttk.Combobox(root, values=minutes, width=5)
minute_combobox.place(x=100,y=150)
minute_combobox.set("00") # 设置默认值为00
confirm_btn = ttk.Button(root, text="确认选择", command=lambda:on_confirm(root),width=20)
confirm_btn.place(x=50,y=250)
# 设置窗口初始大小
# 运行主循环
root.mainloop()
if __name__=="__main__":
window1()
sys.exit()
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...