python_根据本地时间和网络时间自动关机_windows10

下载使用豆瓣源

pip install * -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple

打包python为exe

pyinstaller -F -w Untitled-1.py
-F打包单个文件
-w不弹黑窗
import sys
import os
import requests
import time
import datetime
import ctypes

import win32console, win32gui, win32con
from PySide6 import QtCore, QtWidgets, QtGui

class WinForm(QtWidgets.QWidget):
    def __init__(self,parent=None):
        super(WinForm, self).__init__(parent)
        #设置标题
        self.setWindowTitle('QTimer demo')
 
        #实例化一些控件
        self.listFile=QtWidgets.QListWidget()
        self.lable=QtWidgets.QLabel('显示当前时间')
        self.startBtn=QtWidgets.QPushButton('开始')
        self.endBtn=QtWidgets.QPushButton('结束')
 
        #栅格布局
        layout=QtWidgets.QGridLayout()
 
        #初始化一个定时器,每一秒获取本地时间
        self.timer=QtCore.QTimer()
        #定时器结束,触发showTime方法
        self.timer.timeout.connect(self.showTime)
         #初始化一个定时器,每1min获取网络时间
        self.timer1=QtCore.QTimer()
        self.timer1.timeout.connect(self.change_time)
        #添加控件到栅格指定位置
        layout.addWidget(self.lable,0,0,1,2)
        layout.addWidget(self.startBtn,1,0)
        layout.addWidget(self.endBtn,1,1)
 
        self.startTimer()

 
        #设置布局方式
        self.setLayout(layout)
    def showTime(self):
        #获取系统当前时间
        time=QtCore.QDateTime.currentDateTime()
        #设置系统时间的显示格式
        timeDisplay=time.toString('yyyy-MM-dd hh:mm:ss dddd')
        #在标签上显示时间
        self.lable.setText(timeDisplay)
        #获取小时时间
        hour_time = int(time.toString('hh'))
        #判断时间,在23点之后,8点之前关机
        if hour_time >= 22 or hour_time < 8:
            # print("local_time shutdown")
            os.system("shutdown -s -t  00 ")
    def startTimer(self):
        #设置时间间隔并启动定时器
        self.timer.start(1000)   #1s
        self.timer1.start(60000) #1min
        #设置开始按钮不可点击,结束按钮可点击
        self.startBtn.setEnabled(False)
        self.endBtn.setEnabled(True)
 
    def endTimer(self):
        #停止定时器
        self.timer.stop()
        #结束按钮不可点击,开始按钮可以点击
        self.startBtn.setEnabled(True)
        self.endBtn.setEnabled(False)
    def change_time(self):
        url = 'https://beijing-time.org/'
        rs = requests.get(url=url)
        if rs.status_code == 200:
            header = rs.headers
            net_date = header.get("date")
            gmt_time = time.strptime(net_date[5:25], "%d %b %Y %H:%M:%S")
            bj_time = int(time.mktime(gmt_time) + 8 * 60 * 60)
            tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst = gmt_time
            tm_hour += 8 #转为北京时间,东八区

            if tm_hour >= 22 or tm_hour < 8:
                os.system("shutdown -s -t  00 ")
                #  print("net_time shutdown")
            print(tm_hour)
        

def is_admin():
    try:
        #获取当前用户的是否为管理员
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return True

if __name__ == '__main__':
    app=QtWidgets.QApplication(sys.argv)
    form=WinForm()
    form.show()
    #隐藏黑色框的关闭按钮
    hwnd = win32console.GetConsoleWindow()
    if hwnd:
        hMenu = win32gui.GetSystemMenu(hwnd, 0)
        if hMenu:
            win32gui.DeleteMenu(hMenu, win32con.SC_CLOSE, win32con.MF_BYCOMMAND)
    sys.exit(app.exec())
    # else:
    #     # 重新运行这个程序使用管理员权限
    #     ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
    #     print("admin reboot")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值