import string
import sys
import traceback
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Main(QDialog):
    """定时器Demo"""
    def __init__(self, parent=None):
        try:
            super(Main, self).__init__(parent)
            self.init_ui()
            self.init_slot()
        except:
            traceback.print_exc()

    def init_ui(self):
        try:
            self.resize(480, 240)
            self.setWindowTitle("文件选择demo")
            self.setFixedSize(self.size())

            # 文本框
            self.text1 = QPlainTextEdit(self)
            self.text2 = QPlainTextEdit(self)
            self.text3 = QPlainTextEdit(self)
            self.text4 = QPlainTextEdit(self)

            # 选择按钮
            self.pushButton1 = QPushButton(self)
            self.pushButton1.setText("单文件选择")

            self.pushButton2 = QPushButton(self)
            self.pushButton2.setText("多文件选择")

            self.pushButton3 = QPushButton(self)
            self.pushButton3.setText("文件夹选择")

            self.pushButton4 = QPushButton(self)
            self.pushButton4.setText("文件保存")

            # 布局
            layout1 = QHBoxLayout()
            layout1.addWidget(self.text1)
            layout1.addWidget(self.pushButton1)

            layout2 = QHBoxLayout()
            layout2.addWidget(self.text2)
            layout2.addWidget(self.pushButton2)

            layout3 = QHBoxLayout()
            layout3.addWidget(self.text3)
            layout3.addWidget(self.pushButton3)

            layout4 = QHBoxLayout()
            layout4.addWidget(self.text4)
            layout4.addWidget(self.pushButton4)

            layout = QVBoxLayout()
            layout.addLayout(layout1)
            layout.addLayout(layout2)
            layout.addLayout(layout3)
            layout.addLayout(layout4)

            self.setLayout(layout)
        except:
            traceback.print_exc()

    def init_slot(self):
        try:
            self.pushButton1.clicked.connect(self.select_file)
            self.pushButton2.clicked.connect(self.select_files)
            self.pushButton3.clicked.connect(self.select_dir)
            self.pushButton4.clicked.connect(self.save_file)
        except:
            traceback.print_exc()

    def select_file(self):
        try:
            file, _ = QFileDialog.getOpenFileName(parent=self,
                                                  caption="选择一个文件",  # 标题
                                                  directory=".",  # 打开目录
                                                  filter="All Files (*)"  # 过滤类型
                                                  )
            if file:
                self.text1.setPlainText(file)
        except:
            traceback.print_exc()

    def select_files(self):
        try:
            files, ok = QFileDialog.getOpenFileNames(parent=self,
                                                      caption="选择多个文件",  # 标题
                                                      directory=".",  # 打开目录
                                                      filter="All Files (*)"  # 过滤类型
                                                      )
            if files:
                self.text2.setPlainText('\n'.join(files))
        except:
            traceback.print_exc()

    def select_dir(self):
        try:
            path = QFileDialog.getExistingDirectory(parent=self,
                                                    caption="请选择文件夹",
                                                    directory="."
                                                    )
            if path:
                self.text3.setPlainText(path)
        except:
            traceback.print_exc()

    def save_file(self):
        try:
            path, ok = QFileDialog.getSaveFileName(caption="请选择文件夹",
                                                    directory="./123.txt",
                                                    filter = "All Files (*)"
                                                    )
            if path:
                self.text4.setPlainText(path)
        except:
            traceback.print_exc()


if __name__ == '__main__':
    authorization = None
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())
