python+pyside2写一个windows定时关机小工具

废话不多说,直接上源码

python
from PySide2.QtWidgets import QApplication, QMessageBox
from PySide2.QtUiTools import QUiLoader
import os

class Stats:

    def __init__(self):
        self.ui = QUiLoader().load('main.ui')
        self.ui.pushButton_2.clicked.connect(self.cancel)
        self.ui.pushButton.clicked.connect(self.close)

    def close(self):
        res = int(self.ui.spinBox.text()) * 60
        if res==0:
            choice = QMessageBox.question(
                self.ui,
                '确认',
                '确定要立即关机吗?')
            if choice == QMessageBox.Yes:
                cmd = "shutdown -s -t " + str(res)
                os.popen(cmd)
        else:
            choice = QMessageBox.question(
                self.ui,
                '确认',
                '确定要立即关机吗?')
            if choice == QMessageBox.Yes:
                cmd = "shutdown -s -t " + str(res)
                os.popen(cmd)

    def cancel(self):
        choice = QMessageBox.question(
            self.ui,
            '确认',
            '确定要取消关机吗?')
        if choice == QMessageBox.Yes:
            cmd = "shutdown -a"
            os.popen(cmd)


app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()

main.ui代码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>265</width>
    <height>152</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_2">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_3">
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QLabel" name="label">
       <property name="text">
        <string>定时关机</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_2">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="label_2">
       <property name="text">
        <string>请输入需要将在多少分钟后系统自动关机</string>
       </property>
      </widget>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <widget class="QSpinBox" name="spinBox"/>
       </item>
       <item>
        <widget class="QLabel" name="label_3">
         <property name="text">
          <string>分钟</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QPushButton" name="pushButton">
         <property name="text">
          <string>关机</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_2">
         <property name="text">
          <string>取消关机</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值