PyQt/Demo/RestartWindow.py

63 lines
1.9 KiB
Python
Raw Normal View History

2018-10-28 00:24:47 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2021-07-13 14:52:26 +08:00
"""
2018-10-28 00:24:47 +08:00
Created on 2018年1月17日
2021-07-13 14:52:26 +08:00
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
2018-10-28 00:24:47 +08:00
@email: 892768447@qq.com
2019-01-01 17:04:10 +08:00
@file: RestartWindow
@description: 窗口重启
2021-07-13 14:52:26 +08:00
"""
2018-10-28 00:24:47 +08:00
2021-07-13 14:52:26 +08:00
try:
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QLineEdit, \
QMessageBox, QApplication
except ImportError:
from PySide2.QtCore import Signal as pyqtSignal
from PySide2.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QLineEdit, \
QMessageBox, QApplication
2018-10-28 00:24:47 +08:00
2019-01-01 17:04:10 +08:00
class RestartWindow(QWidget):
2018-10-28 00:24:47 +08:00
restarted = pyqtSignal(QWidget, str)
_Self = None # 很重要,保留窗口引用
def __init__(self, path, *args, **kwargs):
2019-01-01 17:04:10 +08:00
super(RestartWindow, self).__init__(*args, **kwargs)
RestartWindow._Self = self
2018-10-28 00:24:47 +08:00
layout = QVBoxLayout(self)
layout.addWidget(QLabel("当前工作目录:" + path, self))
self.dirEdit = QLineEdit(
self, placeholderText="请输入要切换的目录", returnPressed=self.onChangeDir)
layout.addWidget(self.dirEdit)
layout.addWidget(QPushButton(
"点我切换工作目录", self, clicked=self.onChangeDir))
2019-01-01 17:04:10 +08:00
self.restarted.connect(RestartWindow.onRestart)
2018-10-28 00:24:47 +08:00
def onChangeDir(self):
path = self.dirEdit.text().strip()
if path and QMessageBox.question(self, "提示", "确认要切换到{0}目录吗?".format(path)) == QMessageBox.Yes:
self.hide() # 先隐藏
self.restarted.emit(self, path)
else:
self.dirEdit.setFocus()
@classmethod
def onRestart(cls, widget, path):
2019-01-01 17:04:10 +08:00
w = RestartWindow(path)
2018-10-28 00:24:47 +08:00
w.show()
widget.close()
widget.deleteLater()
del widget
if __name__ == "__main__":
import sys
2021-07-13 14:52:26 +08:00
2018-10-28 00:24:47 +08:00
app = QApplication(sys.argv)
2019-01-01 17:04:10 +08:00
w = RestartWindow("test")
2018-10-28 00:24:47 +08:00
w.show()
sys.exit(app.exec_())