pyqt5 文件路径 绑定到指定输入框
要将文件路径绑定到指定的输入框,你可以使用QFileDialog来选择文件,并将选定的文件路径设置为输入框的文本。
下面是一个示例代码:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QFileDialog
class FileDialogExample(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
self.file_path_input = QLineEdit()
layout.addWidget(self.file_path_input)
select_file_btn = QPushButton('选择文件')
select_file_btn.clicked.connect(self.openFileDialog)
layout.addWidget(select_file_btn)
self.setLayout(layout)
def openFileDialog(self):
file_path, _ = QFileDialog.getOpenFileName(self, '选择文件')
self.file_path_input.setText(file_path)
if __name__ == '__main__':
app = QApplication([])
window = FileDialogExample()
window.show()
app.exec_()
这个例子创建了一个窗口,其中包含一个输入框和一个按钮。当点击按钮时,会打开一个文件对话框,选择文件后,文件路径会显示在输入框中。
你可以将这个示例代码保存为一个Python文件,然后运行它来看到效果
原文地址: https://www.cveoy.top/t/topic/isXE 著作权归作者所有。请勿转载和采集!