| 21 | |
| 22 | |
| 23 | class RestartWindow(QWidget): |
| 24 | restarted = pyqtSignal(QWidget, str) |
| 25 | _Self = None # 很重要,保留窗口引用 |
| 26 | |
| 27 | def __init__(self, path, *args, **kwargs): |
| 28 | super(RestartWindow, self).__init__(*args, **kwargs) |
| 29 | RestartWindow._Self = self |
| 30 | layout = QVBoxLayout(self) |
| 31 | layout.addWidget(QLabel("当前工作目录:" + path, self)) |
| 32 | self.dirEdit = QLineEdit( |
| 33 | self, placeholderText="请输入要切换的目录", returnPressed=self.onChangeDir) |
| 34 | layout.addWidget(self.dirEdit) |
| 35 | layout.addWidget(QPushButton( |
| 36 | "点我切换工作目录", self, clicked=self.onChangeDir)) |
| 37 | self.restarted.connect(RestartWindow.onRestart) |
| 38 | |
| 39 | def onChangeDir(self): |
| 40 | path = self.dirEdit.text().strip() |
| 41 | if path and QMessageBox.question(self, "提示", "确认要切换到{0}目录吗?".format(path)) == QMessageBox.Yes: |
| 42 | self.hide() # 先隐藏 |
| 43 | self.restarted.emit(self, path) |
| 44 | else: |
| 45 | self.dirEdit.setFocus() |
| 46 | |
| 47 | @classmethod |
| 48 | def onRestart(cls, widget, path): |
| 49 | w = RestartWindow(path) |
| 50 | w.show() |
| 51 | widget.close() |
| 52 | widget.deleteLater() |
| 53 | del widget |
| 54 | |
| 55 | |
| 56 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected