来自WeTab AI Pro
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog
import sys
class mainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 500, 300)
self.btn = QPushButton("选择文件", self)
self.btn.clicked.connect(self.openFileDialog)
def openFileDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog # 强制使用 PySide6 提供的文件选择对话框而不是系统原生的对话框
file_name, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "All Files (*)", options=options) # 返回文件名
# file_name, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "All Files (*)") # 这个返回的是系统原生的文件选择对话框
if file_name:
print(f"选择的文件路径为: {file_name}")
if __name__ == "__main__":
app = QApplication([])
window = mainWindow()
window.show()
sys.exit(app.exec())