python 使用PyQt5在ui之间切换

0g0grzrc  于 2023-06-04  发布在  Python
关注(0)|答案(1)|浏览(161)

当我点击btn_choose_threshold按钮时,我想从主用户界面访问另一个用户界面,但当我点击此按钮时,第二个用户界面根本不显示,主用户界面也消失了
有代码可以理解更多。我在www.example.com工作code1.py
mainwindow.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
        
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(1404, 860)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(1150, 860))
        MainWindow.setMaximumSize(QtCore.QSize(1500, 860))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        MainWindow.setFont(font)
        MainWindow.setFocusPolicy(QtCore.Qt.NoFocus)
        icon = QtGui.QIcon.fromTheme("TimeSires")
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lab_browse = QtWidgets.QLabel(self.centralwidget)
        self.lab_browse.setGeometry(QtCore.QRect(10, 10, 151, 21))
        font = QtGui.QFont()
        font.setFamily("Ubuntu Condensed")
        font.setPointSize(13)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.lab_browse.setFont(font)
        self.lab_browse.setWordWrap(False)
        self.lab_browse.setObjectName("lab_browse")
        self.btn_Browse = QtWidgets.QPushButton(self.centralwidget)
        self.btn_Browse.setGeometry(QtCore.QRect(170, 10, 71, 20))
        font = QtGui.QFont()
        font.setFamily("Ubuntu Condensed")
        font.setPointSize(13)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.btn_Browse.setFont(font)
        self.btn_Browse.setObjectName("btn_Browse")
        self.lab_data_det = QtWidgets.QLabel(self.centralwidget)
        self.lab_data_det.setGeometry(QtCore.QRect(890, 10, 111, 16))
        font = QtGui.QFont()
        font.setFamily("Ubuntu Condensed")
        font.setPointSize(13)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.lab_data_det.setFont(font)
        self.lab_data_det.setObjectName("lab_data_det")
        self.table_data = QtWidgets.QTableView(self.centralwidget)
        self.table_data.setGeometry(QtCore.QRect(510, 40, 981, 371))
        self.table_data.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.table_data.setObjectName("table_data")
        self.lab_data_vis = QtWidgets.QLabel(self.centralwidget)
        self.lab_data_vis.setGeometry(QtCore.QRect(300, 0, 231, 31))
        font = QtGui.QFont()
        font.setFamily("Ubuntu Condensed")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.lab_data_vis.setFont(font)
        self.lab_data_vis.setObjectName("lab_data_vis")
        self.line = QtWidgets.QFrame(self.centralwidget)
        self.line.setGeometry(QtCore.QRect(0, 490, 1681, 20))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.line_2 = QtWidgets.QFrame(self.centralwidget)
        self.line_2.setGeometry(QtCore.QRect(230, 500, 16, 301))
        self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.line_4 = QtWidgets.QFrame(self.centralwidget)
        self.line_4.setGeometry(QtCore.QRect(500, 500, 16, 301))
        self.line_4.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_4.setObjectName("line_4")
        self.line_7 = QtWidgets.QFrame(self.centralwidget)
        self.line_7.setGeometry(QtCore.QRect(790, 500, 16, 301))
        self.line_7.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_7.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_7.setObjectName("line_7")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(20, 510, 202, 285))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout_5.setSizeConstraint(QtWidgets.QLayout.SetMaximumSize)
        self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_5.setSpacing(15)
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.label_jac = QtWidgets.QLabel(self.layoutWidget)
        self.label_jac.setMinimumSize(QtCore.QSize(100, 30))
        self.label_jac.setMaximumSize(QtCore.QSize(16777215, 20))
        font = QtGui.QFont()
        font.setFamily("Comic Sans MS")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.label_jac.setFont(font)
        self.label_jac.setAlignment(QtCore.Qt.AlignCenter)
        self.label_jac.setObjectName("label_jac")
        self.verticalLayout_5.addWidget(self.label_jac)
        self.table_jac = QtWidgets.QTableView(self.layoutWidget)
        self.table_jac.setObjectName("table_jac")
        self.verticalLayout_5.addWidget(self.table_jac)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.verticalLayout_5.addLayout(self.horizontalLayout_2)
        self.btn_jac = QtWidgets.QPushButton(self.layoutWidget)
        self.btn_jac.setObjectName("btn_jac")
        self.verticalLayout_5.addWidget(self.btn_jac)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout_5.addLayout(self.horizontalLayout)
        self.line_3 = QtWidgets.QFrame(self.centralwidget)
        self.line_3.setGeometry(QtCore.QRect(0, 790, 1681, 20))
        self.line_3.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_3.setObjectName("line_3")
        self.layoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget_2.setGeometry(QtCore.QRect(560, 510, 201, 281))
        self.layoutWidget_2.setObjectName("layoutWidget_2")
        self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.layoutWidget_2)
        self.verticalLayout_8.setSizeConstraint(QtWidgets.QLayout.SetMaximumSize)
        self.verticalLayout_8.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_8.setSpacing(15)
        self.verticalLayout_8.setObjectName("verticalLayout_8")
        self.lab_group2 = QtWidgets.QLabel(self.layoutWidget_2)
        self.lab_group2.setMinimumSize(QtCore.QSize(100, 30))
        self.lab_group2.setMaximumSize(QtCore.QSize(16777215, 20))
        font = QtGui.QFont()
        font.setFamily("Comic Sans MS")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.lab_group2.setFont(font)
        self.lab_group2.setAlignment(QtCore.Qt.AlignCenter)
        self.lab_group2.setObjectName("lab_group2")
        self.verticalLayout_8.addWidget(self.lab_group2)
        self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")
        self.list_group2 = QtWidgets.QListView(self.layoutWidget_2)
        self.list_group2.setObjectName("list_group2")
        self.horizontalLayout_6.addWidget(self.list_group2)
        self.verticalLayout_8.addLayout(self.horizontalLayout_6)
        self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_7.setObjectName("horizontalLayout_7")
        self.btn_group2 = QtWidgets.QPushButton(self.layoutWidget_2)
        self.btn_group2.setObjectName("btn_group2")
        self.horizontalLayout_7.addWidget(self.btn_group2)
        self.verticalLayout_8.addLayout(self.horizontalLayout_7)
        self.line_5 = QtWidgets.QFrame(self.centralwidget)
        self.line_5.setGeometry(QtCore.QRect(1050, 500, 16, 301))
        self.line_5.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_5.setObjectName("line_5")
        self.line_6 = QtWidgets.QFrame(self.centralwidget)
        self.line_6.setGeometry(QtCore.QRect(240, 0, 16, 421))
        self.line_6.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_6.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_6.setObjectName("line_6")
        self.line_9 = QtWidgets.QFrame(self.centralwidget)
        self.line_9.setGeometry(QtCore.QRect(0, 410, 1681, 20))
        self.line_9.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_9.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_9.setObjectName("line_9")
        self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget1.setGeometry(QtCore.QRect(270, 510, 193, 281))
        self.layoutWidget1.setObjectName("layoutWidget1")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.layoutWidget1)
        self.verticalLayout_4.setSizeConstraint(QtWidgets.QLayout.SetMaximumSize)
        self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_4.setSpacing(15)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.lab_group1 = QtWidgets.QLabel(self.layoutWidget1)
        self.lab_group1.setMinimumSize(QtCore.QSize(0, 30))
        self.lab_group1.setMaximumSize(QtCore.QSize(16777215, 30))
        font = QtGui.QFont()
        font.setFamily("Comic Sans MS")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.lab_group1.setFont(font)
        self.lab_group1.setAlignment(QtCore.Qt.AlignCenter)
        self.lab_group1.setObjectName("lab_group1")
        self.verticalLayout_4.addWidget(self.lab_group1)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.list_group1 = QtWidgets.QListView(self.layoutWidget1)
        self.list_group1.setObjectName("list_group1")
        self.horizontalLayout_3.addWidget(self.list_group1)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.btn_group1 = QtWidgets.QPushButton(self.layoutWidget1)
        self.btn_group1.setObjectName("btn_group1")
        self.verticalLayout.addWidget(self.btn_group1)
        self.verticalLayout_4.addLayout(self.verticalLayout)
        self.layoutWidget2 = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget2.setGeometry(QtCore.QRect(260, 40, 241, 361))
        self.layoutWidget2.setObjectName("layoutWidget2")
        self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.layoutWidget2)
        self.verticalLayout_6.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_6.setObjectName("verticalLayout_6")
        self.btn_choose_threshold = QtWidgets.QPushButton(self.layoutWidget2)
        self.btn_choose_threshold.setObjectName("btn_choose_threshold")
        
        self.verticalLayout_6.addWidget(self.btn_choose_threshold)
        self.horizontalLayout_11 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_11.setObjectName("horizontalLayout_11")
        self.btn_bar = QtWidgets.QPushButton(self.layoutWidget2)
        self.btn_bar.setObjectName("btn_bar")
        self.horizontalLayout_11.addWidget(self.btn_bar)
        self.btn_cloud = QtWidgets.QPushButton(self.layoutWidget2)
        self.btn_cloud.setObjectName("btn_cloud")
        self.horizontalLayout_11.addWidget(self.btn_cloud)
        self.verticalLayout_6.addLayout(self.horizontalLayout_11)
        self.horizontalLayout_12 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_12.setObjectName("horizontalLayout_12")
        self.btn_treemap = QtWidgets.QPushButton(self.layoutWidget2)
        self.btn_treemap.setObjectName("btn_treemap")
        self.horizontalLayout_12.addWidget(self.btn_treemap)
        self.verticalLayout_6.addLayout(self.horizontalLayout_12)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 1404, 21))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.actionData_Visualisation = QtWidgets.QAction(MainWindow)
        self.actionData_Visualisation.setObjectName("actionData_Visualisation")
        self.actionTrain_Data = QtWidgets.QAction(MainWindow)
        self.actionTrain_Data.setObjectName("actionTrain_Data")

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.lab_browse.setText(_translate("MainWindow", "Select Data File"))
        self.btn_Browse.setText(_translate("MainWindow", "Browse"))
        self.lab_data_det.setText(_translate("MainWindow", "Data Details"))
        self.lab_data_vis.setText(_translate("MainWindow", "Data Base Visualisation "))
        self.label_jac.setText(_translate("MainWindow", "Jaccard Distance"))
        self.btn_jac.setText(_translate("MainWindow", "Show Jaccard "))
        self.lab_group2.setText(_translate("MainWindow", "Group 2"))
        self.btn_group2.setText(_translate("MainWindow", "Show Group 2"))
        self.lab_group1.setText(_translate("MainWindow", "Group 1"))
        self.btn_group1.setText(_translate("MainWindow", "Show Group 1"))
        self.btn_choose_threshold.setText(_translate("MainWindow", "Choose Threshold"))
        self.btn_bar.setText(_translate("MainWindow", "Bar Chart"))
        self.btn_cloud.setText(_translate("MainWindow", "Word Cloud"))
        self.btn_treemap.setText(_translate("MainWindow", "Treemap"))
        self.actionData_Visualisation.setText(_translate("MainWindow", "Data Visualisation"))
        self.actionTrain_Data.setText(_translate("MainWindow", "Train Data"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

threshold.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Threshold(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(303, 188)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lab_threshold = QtWidgets.QLabel(self.centralwidget)
        self.lab_threshold.setGeometry(QtCore.QRect(50, 40, 150, 40))
        self.lab_threshold.setMaximumSize(QtCore.QSize(150, 40))
        font = QtGui.QFont()
        font.setFamily("Ubuntu Condensed")
        font.setPointSize(13)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.lab_threshold.setFont(font)
        self.lab_threshold.setWordWrap(False)
        self.lab_threshold.setObjectName("lab_threshold")
        self.edit_threshold = QtWidgets.QLineEdit(self.centralwidget)
        self.edit_threshold.setGeometry(QtCore.QRect(160, 40, 50, 40))
        self.edit_threshold.setMinimumSize(QtCore.QSize(20, 20))
        self.edit_threshold.setMaximumSize(QtCore.QSize(50, 40))
        self.edit_threshold.setBaseSize(QtCore.QSize(20, 20))
        font = QtGui.QFont()
        font.setFamily("Comic Sans MS")
        self.edit_threshold.setFont(font)
        self.edit_threshold.setAlignment(QtCore.Qt.AlignCenter)
        self.edit_threshold.setReadOnly(True)
        self.edit_threshold.setObjectName("edit_threshold")
        self.btn_ok = QtWidgets.QPushButton(self.centralwidget)
        self.btn_ok.setGeometry(QtCore.QRect(60, 100, 151, 20))
        self.btn_ok.setObjectName("btn_ok")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 303, 21))
        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", "MainWindow"))
        self.lab_threshold.setText(_translate("MainWindow", "Threshold"))
        self.btn_ok.setText(_translate("MainWindow", "OK"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Threshold()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

code1.py

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, uic
import pandas as pd
from PyQt5.QtCore import QAbstractTableModel, Qt
from PyQt5.QtWidgets import QScrollArea
from PyQt5.QtWidgets import QInputDialog, QLineEdit, QDialog, QWidget, QLabel, QVBoxLayout

class PandasModel(QAbstractTableModel):
    def __init__(self, data):
        QAbstractTableModel.__init__(self)
        self._data = data

    def data(self, index, role):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def rowCount(self, parent=None):
        return len(self._data.index)

    def columnCount(self, parent=None):
        return len(self._data.columns)

    def headerData(self, section, orientation, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return str(self._data.columns[section])
            if orientation == Qt.Vertical:
                return str(self._data.index[section])
        return None


class ThresholdWindow(QDialog):#
    def __init__(self):
        super(ThresholdWindow, self).__init__()
        loadUi("threshold.ui", self)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        loadUi("Mainwindow.ui", self)
        self.btn_Browse.clicked.connect(self.browse_file)
        self.btn_choose_threshold.clicked.connect(self.open_threshold_window)

        
    def browse_file(self):
        file_dialog = QFileDialog()
        filepath, _ = file_dialog.getOpenFileName(self, 'Select Excel File', '', 'Excel files (*.xlsx; *.xls)')
        if filepath:
            df = pd.read_excel(filepath)
            model = PandasModel(df)
            self.table_data.setModel(model)

    def open_threshold_window(self):
        print("Opening Threshold Window")
        self.threshold_window = ThresholdWindow()
        self.threshold_window.show()

if __name__ == "__main__":
    

    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())
kknvjkwl

kknvjkwl1#

我不知道这个指南对你有什么帮助,但也许在第47行code1.py不能加载mainwindow.ui并说没有这样的文件。

相关问题