# -*- coding: utf-8 -*-
##main.py 主要负责写业务逻辑的文件。
import sys, os
from PySide2 import QMainWindow, QFileDialog, QApplication
from PySide2 import QtGui, QtWidgets, QtCore
import design
# from string import maketrans
# 创建APP类,并继承UI的类
class WoHeYunApp(QMainWindow, design.Ui_MainWindow):
# 初始化构造函数
def __init__(self):
# 继承: super指父类(子类,实例).构造函数
super(WoHeYunApp, self).__init__()
self.setupUi(self)
# 把UI中的函数功能(事件)
self.pushButton.clicked.connect(self.browse_folder)
self.pushButton2.clicked.connect(self.click_event)
# self.openMessageBox()
# 功能函数
def browse_folder(self):
self.listWidget.clear() # 清除列表内容
a = QFileDialog # 创建文件夹选择对话框
# a.setDirectoryUrl(self,'E:\\')
# QFileDialog.setDirectory(QFileDialog, )
directory = a.getExistingDirectory(self, "选择一个mysql转换为gray")
directory2 = a.getOpenFileNames(self,'Pick a folder')
item = self.listWidget.currentItem();
print(item)
path = directory2[0][0] ##输出的得到文件路径
path2 = path.replace('MYSQL','GRAY') #输入到的文件路径
# print(directory2[0][0])
# print('5')
fi = open(path, "r", encoding="utf-8")
fo = open(path2, "w", encoding="utf-8")
# os.rename("%s.bak" % path2, path2)?
# os.remove(path2)
# os.rename("%s.bak" % path2, path2)
ms ='是否将' + directory2[0][0]+'文件中的mysql 更换成gray'
ms2 ='更改完毕'
ms3 = '是否打开' + path2
if QFileDialog.fileSelected :
print(self.openMessageBox(ms))
if self.openMessageBox(ms) == 1:
for line in fi.readlines(): # 逐行遍历
# print(line)
line = line.replace('MYSQL', 'GRAY') # 构造(i)并替换
# print(line)
fo.write(line)
self.openMessageBox(ms2)
self.openMessageBox(ms3)
# 保存按钮被点击
# os.remove(fo)
# os.rename("%s.bak" % fo, fo)
# fo.close()
# # 如果有范围的选择路径则搜索该路径下所有存在的文件,并将其名字加载都列表中
# if directory:
# for file_name in os.listdir(directory):
# self.listWidget.addItem(file_name)
# print('7')
# print(file_name)
# else:
# print( QFileDialog.getOpenFileNames(self, 'Pick a folder')[0])
# print('6')
self.listWidget.addItem('你访问文件问:'+ directory2[0][0])
fi.close()
fo.close()
def click_event(self):
# print("hello world")
# 清除列表内容
self.listWidget.clear()
# 创建文件夹选择对话框
a = QFileDialog
directory = a.getExistingDirectory(self, "Pick a folder")
# print('1')
# print(type(directory))
# print('2')
print(directory) ## 文件 夹路径
directory2 = a.getOpenFileNames(self,'Pick a folder')
a.clicked.connect(self.openMessageBox)
path = directory2[0][0] ##输出的得到文件路径
path2 = path.replace('.kjb','.xml') #输入到的文件路径
# print(directory2[0][0])
# print('5')
fi = open(path, "r", encoding="utf-8")
fo = open(path2, "w", encoding="utf-8")
# os.rename("%s.bak" % path2, path2)?
# os.remove(path2)
# os.rename("%s.bak" % path2, path2)
for line in fi.readlines(): #逐行遍历
# print(line)
line=line.replace('MYSQL','GREY') #构造(i)并替换
# print(line)
fo.write(line)
# os.remove(fo)
os.rename("%s.xml" % fo, fo)
# fo.close()
# # 如果有范围的选择路径则搜索该路径下所有存在的文件,并将其名字加载都列表中
# if directory:
# for file_name in os.listdir(directory):
# self.listWidget.addItem(file_name)
# print('7')
# print(file_name)
# else:
# print( QFileDialog.getOpenFileNames(self, 'Pick a folder')[0])
# print('6')
self.listWidget.addItem('你访问文件问:'+ directory2[0][0])
fi.close()
fo.close()
# 创建实例
def main():
# 创建新的实例应用
app = QApplication(sys.argv)
# 我们将表单设置为WoHeYunApp
widgets = WoHeYunApp()
# 显示我们的表单
widgets.show()
# 退出程序
app.exec_()
# 如果我们直接运行文件而不是导入它,则执行
if __name__ == '__main__':
main()