python

1、ini参数设置
if not os.path.exists('setting.ini'):
    config = configparser.ConfigParser()
    config.add_section('Directories')
    config.set('Directories', 'last_used_directory', 'c:')
    with open('setting.ini', 'w') as configfile:
        config.write(configfile)
config = configparser.ConfigParser()
config.read('setting.ini')
last_used_directory = config.get('Directories', 'last_used_directory')

2、打开目录

def get_directory(self, last_used_directory):
    # root = tk.Toplevel()
    # root.withdraw()
    if not last_used_directory:
        last_used_directory = os.getcwd()
    directory = filedialog.askdirectory( initialdir=last_used_directory,)

    return directory

3、ui界面 简单界面,直接用代码实现

class MyApp(QApplication):
    FileName=""

    def __init__(self, argv):
        super().__init__(argv)
        self.loop = asyncio.get_event_loop()
        self.queue = asyncio.Queue()

        self.timer = QTimer()
        self.timer.timeout.connect(self.update_text)
        self.timer.start(100)
        self.initUI()

    def initUI(self):
        self.window = QWidget()
        self.log = QTextEdit()
        self.btnOpenFile = QPushButton('打开文件')
        self.btnOpenFile.clicked.connect(self.browseFile)
        self.btnOutputDir = QPushButton('设置输出目录')
        self.btnOutputDir.clicked.connect(self.setOutputDir)
        self.btnMake = QPushButton('生成语句')
        self.btnMake.clicked.connect(self.on_click)

        self.hbox = QHBoxLayout()
        # hbox = QHBoxLayout()

        self.hbox.addWidget(self.btnOpenFile)
        self.hbox.addWidget(self.btnOutputDir)
        self.hbox.addWidget(self.btnMake)

        self.vbox = QVBoxLayout()
        # vbox = QVBoxLayout()
        self.vbox.addLayout(self.hbox)
        self.vbox.addWidget(self.log)


        self.window.setLayout(self.vbox)
        self.window.setWindowTitle('数据处理')
        self.window.setGeometry(300, 300, 800, 1000)
        self.window.setWindowIcon(QIcon('icon.png'))
        self.window.show()

if __name__ == '__main__':
    import sys
    app = MyApp(sys.argv)
    sys.exit(app.exec_())

4、复杂的界面,在qtcreator 添加文件-》 qt-》Qt Designer Form

直接加界面和槽函数,参考Pyqt5一小时速成精髓_哔哩哔哩_bilibili中的处理方法。

# This Python file uses the following encoding: utf-8
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QWidget,QAbstractItemView
from PyQt5.Qt import QWidget,QApplication,QTableWidgetItem
from PyQt5.uic import loadUi
from PyQt5 import QtCore,QtGui

from PyQt5.QtGui import QPixmap


QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) #QtCore.Qt.AA_EnableHighDpiScaling
class MainWindow(QWidget):
    def __init__(self):
        super(QWidget,self).__init__()
        loadUi("./form.ui",self)
    def slot1(self):
        print("aaaaaaaaaaa")
        return
if __name__ == "__main__":
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    app=QApplication.instance()
    if app is None:
        QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)

    Widget=MainWindow()
    Widget.show()


    # ...
    sys.exit(app.exec())

你可能感兴趣的:(python)