PYTHON 搭建一个能够选择文件并进行保存的QT窗体
简单记录一下…
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QCoreApplication
import warnings
warnings.filterwarnings("ignore") # specify to ignore warning messages
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(600, 150)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.datapath = QtWidgets.QLineEdit(self.centralwidget)
self.datapath.setGeometry(QtCore.QRect(180, 20, 400, 30))
self.datapath.setObjectName("datapath")
self.savepath = QtWidgets.QLineEdit(self.centralwidget)
self.savepath.setGeometry(QtCore.QRect(180, 55, 400, 30))
self.savepath.setObjectName("savepath")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 20, 150, 30))
self.pushButton.setStyleSheet("font: 12pt \"Agency FB\";")
self.pushButton.setObjectName("pushButton")
self.pushButton1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton1.setGeometry(QtCore.QRect(20, 55, 150, 30))
self.pushButton1.setStyleSheet("font: 12pt \"Agency FB\";")
self.pushButton1.setObjectName("pushButton1")
self.pushButton2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton2.setGeometry(QtCore.QRect(275, 90, 50, 30))
self.pushButton2.setStyleSheet("font: 12pt \"Agency FB\";")
self.pushButton2.setObjectName("pushButton2")
self.pushButton3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton3.setGeometry(QtCore.QRect(350, 90, 50, 30))
self.pushButton3.setStyleSheet("font: 12pt \"Agency FB\";")
self.pushButton3.setObjectName("pushButton3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 600, 30))
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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "功能"))
# self.label_2.setText(_translate("MainWindow",
# "<html><head/><body><p><span style=\" font-size:48pt;\">+</span></p></body></html>"))
self.pushButton.setText(_translate("MainWindow", "输入路径"))
self.pushButton.clicked.connect(lambda: self.choosedatafile()) # 输入数据的路径选择
self.pushButton1.setText(_translate("MainWindow", "保存路径"))
self.pushButton1.clicked.connect(lambda: self.savedatafile()) # 保存数据的路径选择
self.pushButton2.setText(_translate("MainWindow", "确认"))
self.pushButton2.clicked.connect(lambda: self.process1()) # 运行代码的确认
self.pushButton3.setText(_translate("MainWindow", "关闭"))
self.pushButton3.clicked.connect(QCoreApplication.instance().quit) # 关闭窗口
def choosedatafile(self):
fileName, fileType = QtWidgets.QFileDialog.getOpenFileName(caption="选取文件", filter="CSV Files(*.csv)")#添加了文件格式过滤器,也可删除
self.datapath.setText(fileName)
pass
def savedatafile(self):
fileName=QtWidgets.QFileDialog.getExistingDirectory(None, "选择存储文件夹")
self.savepath.setText(fileName)
pass
def process1(self):#运行功能的代码
datapath=self.datapath.text()
savepath=self.savepath.text()#注意这里savepath只是文件夹名,保存时要加文件名,如+1.xlsx
#function(datapath,savepath)
return datapath,savepath
# main函数
if __name__ == '__main__':
#打开窗口
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
pass```