PyQt/Demo/AutoRestart.py

63 lines
1.6 KiB
Python
Raw Normal View History

2017-03-31 15:34:28 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2021-07-13 14:52:26 +08:00
"""
2017-03-31 15:34:28 +08:00
Created on 2017年3月31日
2021-07-13 14:52:26 +08:00
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
2017-03-31 15:34:28 +08:00
@email: 892768447@qq.com
@file: AutoRestart
@description:
2021-07-13 14:52:26 +08:00
"""
2017-03-31 15:34:28 +08:00
import os
import sys
2021-07-13 14:52:26 +08:00
from optparse import OptionParser
2019-02-01 23:41:22 +08:00
2021-07-13 14:52:26 +08:00
try:
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout
except ImportError:
from PySide2.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout
2019-02-01 23:41:22 +08:00
canRestart = True
2017-03-31 15:34:28 +08:00
def restart(twice):
os.execl(sys.executable, sys.executable, *[sys.argv[0], "-t", twice])
2019-02-01 23:41:22 +08:00
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 400)
layout = QHBoxLayout(self)
self.buttonRestart = QPushButton(
"app start...%s...twice\napp pid: %s\n点击按钮重启...\n" %
(options.twice, os.getpid()), self)
self.buttonRestart.clicked.connect(self.close)
self.buttonExit = QPushButton('退出', self, clicked=self.doExit)
layout.addWidget(self.buttonRestart)
layout.addWidget(self.buttonExit)
def doExit(self):
global canRestart
canRestart = False
self.close()
2017-03-31 15:34:28 +08:00
if __name__ == "__main__":
parser = OptionParser(usage="usage:%prog [optinos] filepath")
2019-02-01 23:41:22 +08:00
parser.add_option("-t", "--twice", type="int",
dest="twice", default=1, help="运行次数")
2017-03-31 15:34:28 +08:00
options, _ = parser.parse_args()
2019-02-01 23:41:22 +08:00
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()
if canRestart:
restart(str(options.twice + 1))