pyqt主窗口在打开另一个窗口时冻结

时间:2019-02-27 10:34:41

标签: python user-interface pyqt

我正在使用PyQt应用,该应用使用熊猫在另一个窗口中显示CSV文件

用户单击添加按钮,然后使用FileDialog选择CSV文件。然后,熊猫将读取数据框,该数据框将传递到另一个显示内容的窗口。

main.py

from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QFileDialog
from text_viewer import TextViewer
import sys
import pandas as pnd
from threading import Thread

class MainUI(QMainWindow):

    def __init__(self):
        super(MainUI, self).__init__()
        self.datas = []
        uic.loadUi('main.ui', self)
        self.model = QStandardItemModel()
        self.listView.setModel(self.model)
        self.load_btn.clicked.connect(self.add_btn)
        self.show()

    def add_btn(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        allowed_extensions = "CSV Files (*.csv)"
        files = QFileDialog.getOpenFileNames(self, "Choose Data File To Load", "", allowed_extensions, options=options)
        if files:
            thread = Thread(target=self.add, args=(files,))
            thread.start()

    def add(self, files):
        for file in files[0]:
            try:

                if file.endswith(".csv"):
                    df = pnd.read_csv(file)
                    viewer = TextViewer(df)

                if df is not None:
                    self.datas.append(df)
                    item = QStandardItem(file)
                    self.model.appendRow(item)


            except Exception as e:
                print(str(e))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainUI()
    sys.exit(app.exec_())

text_viewer.py

from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow


class TextViewer(QMainWindow):

    def __init__(self, df):
        super(TextViewer, self).__init__()
        self.df = df
        uic.loadUi('text_viewer.ui', self)
        self.show() # at this point the main window freezes 

当我选择文件时,将创建一个新的TextViewer实例,然后主窗口冻结。如果我不拨打电话,则什么也没显示,但是如果我这样做,该应用程序将冻结,直到从任务管理器中手动将其关闭为止。

0 个答案:

没有答案