时间:2023-02-20 09:45:52 | 栏目:Python代码 | 点击:次
使用PyQt5开发桌面程序,实现功能为:按下按键,打开文件夹,选择文件夹,并将路径显示出来。
1、选择文件夹并显示
def msg(self,Filepath): directory = QtWidgets.QFileDialog.getExistingDirectory(None,"选取文件夹","C:/") # 起始路径 self.fileT.setText(directory)
2、选择文件
def msg(self,Filepath): directory = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件","./", "All Files (*);;Text Files (*.txt)") #当窗口非继承QtWidgets.QDialog时,self需替换成 None
3、选择多个文件
def msg(self,Filepath): directory = QtWidgets.QFileDialog.getOpenFileNames(self, self, "选取多个文件", "./","All Files (*);;Text Files (*.txt)") #当窗口非继承QtWidgets.QDialog时,self需替换成 None
4、设置保存文件路径
def msg(self,Filepath): directory = QtWidgets.QFileDialog.getSaveFileName(self, "设置路径","./","All Files (*);;Text Files (*.txt)") #当窗口非继承QtWidgets.QDialog时,self需替换成 None
(从项目里摘出来的,不太精致,但运行没问题)
from PyQt5 import QtCore, QtWidgets import sys ########################################## #ui界面设置 class Ui_MainWindow(object): def setupUi(self, MainWindow): #主窗口参数设置 MainWindow.setObjectName("MainWindow") MainWindow.resize(848, 721) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") # 设置按键参数 self.file = QtWidgets.QPushButton(self.centralwidget) self.file.setGeometry(QtCore.QRect(57, 660, 175, 28)) self.file.setObjectName("file") self.file.setStyleSheet(" margin: 0px; padding: 0px;">) self.file.setStyleSheet( "QPushButton{ margin: 0px; padding: 0px;">"QPushButton:hover{color:green}" # 光标移动到上面后的前景色 "QPushButton{border-radius:6px}" # 圆角半径 "QPushButton:pressed{border: None;}" # 按下时的样式 ) # 设置显示窗口参数 self.fileT = QtWidgets.QPushButton(self.centralwidget) self.fileT.setGeometry(QtCore.QRect(300, 660, 480, 28)) self.fileT.setObjectName("file") self.fileT.setStyleSheet(" margin: 0px; padding: 0px;">) self.fileT.setStyleSheet( "QPushButton{ margin: 0px; padding: 0px;">"QPushButton:hover{color:green}" # 光标移动到上面后的前景色 "QPushButton{border-radius:6px}" # 圆角半径 "QPushButton:pressed{border: None;}" # 按下时的样式 ) #主窗口及菜单栏标题栏设置 MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 848, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) ################button按钮点击事件回调函数################ self.file.clicked.connect(self.msg) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "Deecamp_Eurus")) self.file.setText(_translate("MainWindow", "选择文件")) self.fileT.setText(_translate("MainWindow", "")) #########选择图片文件夹######### def msg(self,Filepath): m = QtWidgets.QFileDialog.getExistingDirectory(None,"选取文件夹","C:/") # 起始路径 self.fileT.setText(m) #########主函数入口 ######### if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) mainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(mainWindow) mainWindow.show() sys.exit(app.exec_())
运行效果为:
以上。
补充:pyqt5按钮事件打开文件
self.pushButton.clicked.connect(self.btn1_click)#设置绑定事件 def btn1_click(self): filename,_=QFileDialog.getOpenFileName(QWidget(),'open',r"E:\images1") #打开文件或者如下:getOpenFileName第一个参数要求是QWidget对象 #filename,_=QFileDialog.getOpenFileName(None,'open',r"E:\images1",' ') img=QImage() img.load(filename) #更改图片大小,适应graphicsView窗口大小 img=img.scaled(self.graphicsView.width(),self.graphicsView.height()) #一个问题是:img=img.scaled(width=self.graphicsView.width(),height=self.graphicsView.height())这样 #会出错,不知道什么原因 scene = QGraphicsScene () scene. addPixmap ( QPixmap (). fromImage (img)) self .graphicsView. setScene (scene)