pyqt5制作定时关机软件

pyqt5制作定时关机软件

    • 界面
    • 知识点
      • 禁止窗口改变大小
      • 托盘设置
      • 关机定时器
      • tabwidget页签居中,设置stylesheet
    • Github

界面

pyqt5制作定时关机软件_第1张图片

知识点

禁止窗口改变大小

self.setFixedSize(self.width(), self.height())

托盘设置

def setTuoPang(self):
    # 创建窗口托盘
    self.pTray = QSystemTrayIcon(self)
    # 设置托盘图标样式
    icon = QIcon()
    icon.addPixmap(QPixmap(":/res/icon.png"))
    self.pTray.setIcon(icon)
    # 显示图标
    self.pTray.show()

    quitAction = QAction("&退出吧", self, triggered=QApplication.instance().quit)  # 退出APP
    self.trayMenu = QMenu(self)
    self.trayMenu.addAction(quitAction)
    self.pTray.setContextMenu(self.trayMenu)
    self.pTray.setToolTip("关机软件")
    self.pTray.showMessage("提示", "开启关机定时器")
    self.pTray.messageClicked.connect(self.onTrayMessageClick)
    # #托盘图标被激活
    self.pTray.activated.connect(self.onTrayActivated)

# 界面上关闭按钮
def closeEvent(self, event):
    event.ignore()  # 忽略关闭事件
    self.hide()  # 隐藏窗体

# 托盘图标事件
def onTrayActivated(self, reason):
    print("触发托盘图标事件", reason)
    if reason == QSystemTrayIcon.DoubleClick:  # 双击事件
        self.onTrayDoubleClick()
    elif reason == QSystemTrayIcon.Trigger:  # 单击事件
        self.onTrayTrigger()

def onTrayDoubleClick(self):
    print("双击了托盘")
    if self.isMinimized() or not self.isVisible():
        self.showNormal()  # 正常显示
        self.activateWindow()
    else:
        self.showMinimized()  # 最小化

def onTrayTrigger(self):
    print("点击了托盘")

def onTrayMessageClick(self, *args):
    print("点击了托盘信息")

关机定时器

self.m_oCloseWindowTimer = QTimer()
self.m_oCloseWindowTimer.timeout.connect(self.checkNeedClose)
self.m_oCloseWindowTimer.start(1000)
def checkNeedClose(self):
    fCurTime = time.time()
    if fCurTime > self.m_iCloseTime:
        self.m_oCloseWindowTimer.stop()
        self.closeWindow()
    else:
        iLeftTime = int(self.m_iCloseTime - fCurTime)
        sTime = tools_time.getHourMiniSecDes(iLeftTime)
        self.txtTip.setText("{time}后关闭电脑".format(time=sTime))

def closeWindow(self):
    print("关机了...")
    sCmd = "shutdown -s -t 1"
    subprocess.run(sCmd)

tabwidget页签居中,设置stylesheet

QTabWidget::tab-bar {
    alignment: center;
}

Github

源码

你可能感兴趣的:(PyQt5)