class DownloadDirTreeWidget(QWidget): def __init__(self, root_path) -> None: QWidget.__init__(self) # self.index stores the index of the latest item which is clicked # self.root_path is the path to the folder currently showing self.index = None self.root_path = os.path.abspath(root_path) self.dir_view = QTreeView() self.model = QFileSystemModel(self.dir_view) self.model.setRootPath(self.root_path) self.dir_view.clicked.connect(self.onFileItemClicked) self.dir_view.doubleClicked.connect(self.onFileItemDoubleClicked) self.dir_view.setModel(self.model) self.dir_view.setRootIndex(self.model.index(self.root_path)) open_button = QPushButton("Open") open_button.clicked.connect(self.openFile) open_in_file_explorer_button = QPushButton("Open in File Explorer") open_in_file_explorer_button.clicked.connect(self.openInFileExplorer) self.root_path_line_edit = QLineEdit(self.root_path) self.root_path_line_edit.returnPressed.connect( self.onChangeLineEditReturned) self.root_path_line_edit.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.root_path_line_edit.adjustSize() change_path_button = QPushButton('Change Directory') change_path_button.clicked.connect(self.onChangeButtonClicked) addressCompleter = QCompleter() addressCompleter.setModel(self.model) self.root_path_line_edit.setCompleter(addressCompleter) # Set layout layout = QGridLayout() layout.addWidget(self.root_path_line_edit, 0, 0, 1, 1) layout.addWidget(change_path_button, 0, 1, 1, 1) layout.addWidget(self.dir_view, 1, 0, 1, 2) layout.addWidget(open_button, 2, 0, 1, 1) layout.addWidget(open_in_file_explorer_button, 2, 1, 1, 1) layout.setMargin(0) self.setLayout(layout) def setRootPath(self, root_path): self.root_path = os.path.abspath(root_path) def openFile(self): if self.index is not None: file_path = self.model.filePath(self.index).replace('/', '\\') is_dir = self.model.isDir(self.index) # If is file, open with default program # If is directory, open with file explorer if is_dir is False: os.startfile(file_path, 'open') else: subprocess.run(['explorer', file_path]) def openInFileExplorer(self): if self.index is None: file_path = self.model.filePath(self.index).replace('/', '\\') subprocess.run(['explorer', '/select,', file_path]) def onFileItemClicked(self, index): # When clicked, resize and update self.index self.dir_view.resizeColumnToContents(0) self.index = index def onFileItemDoubleClicked(self, index): # When double clicked, update self.index and open the file directly self.index = index if self.sender().model().isDir(index) is False: self.openFile() def onChangeButtonClicked(self): new_path = QFileDialog.getExistingDirectory(self, 'Change Directory', self.root_path) self.changeRootPath(new_path) def onChangeLineEditReturned(self): new_path = self.root_path_line_edit.text() if os.path.isdir(new_path): self.changeRootPath(new_path) else: subprocess.run(['explorer', new_path]) self.root_path_line_edit.setText(self.root_path) def changeRootPath(self, new_path: str): if os.path.exists(new_path): self.root_path = os.path.abspath(new_path) self.dir_view.setRootIndex(self.model.index(self.root_path)) self.root_path_line_edit.setText(self.root_path)
class Converter(QWidget): def __init__(self): QWidget.__init__(self) self.c = CurrencyConverter(fallback_on_wrong_date=True) self.setWindowTitle('Converter') self.activefirstChoice = QLabel("", self) self.activelastChoice = QLabel("", self) self.firstValue = QLineEdit("0", self) self.lastValue = QLineEdit("0", self) self.firstChoice = QComboBox() self.lastChoice = QComboBox() layout = QHBoxLayout() layout.addWidget(self.firstChoice) layout.addWidget(self.firstValue) layout.addWidget(self.lastChoice) layout.addWidget(self.lastValue) self.setLayout(layout) for key in sorted(self.c.currencies): self.firstChoice.addItem(key) self.lastChoice.addItem(key) self.firstChoice.activated[str].connect(self.onchangefirstChoice) self.firstValue.textChanged[str].connect(self.onchangefirstValue) self.lastChoice.activated[str].connect(self.onchangelastChoice) self.lastValue.textChanged[str].connect(self.onchangelastValue) def onchangefirstChoice(self, text): self.activefirstChoice = text def onchangefirstValue(self, text): if text == "": self.firstValue.setText("0") else: self.lastValue.blockSignals(True) self.firstValue.setText(text) value = str( self.c.convert(float(text), str(self.firstChoice.currentText()), str(self.lastChoice.currentText()))) print(value) self.lastValue.setText(value) self.lastValue.blockSignals(False) self.lastValue.adjustSize() def onchangelastChoice(self, text): self.activelastChoice = text def onchangelastValue(self, text): if text != "": self.firstValue.blockSignals(True) self.lastValue.setText(text) value = str( self.c.convert(float(text), str(self.firstChoice.currentText()), str(self.lastChoice.currentText()))) print(value) self.firstValue.setText(value) self.firstValue.blockSignals(False) self.firstValue.adjustSize() else: self.lastValue.setText("0")