2016-09-24 50 views
1

這是必須調用UI類的類:如何調用UI類QT5在Python

class mainpanelManager(QtGui.QGuiApplication, Ui_MainWindow): 
    def __init__(self): 
     QtGui.QGuiApplication.__init__(self) 
     Ui_MainWindow.__init__(self) 
     self.setupUi(self) 


if __name__ == "__main__": 
    app = QtGui.QGuiApplication(sys.argv) 
    window = mainpanelManager() 
    window.show() 
    sys.exit(app.exec_()) 

這就是UI類(從QtCreator和Qt5.7生成):

from PyQt5 import QtCore, QtGui, QtWidgets 


class Ui_MainWindow(QtGui.QWindow): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName("MainWindow") 
     MainWindow.resize(1155, 704) 
     self.centralwidget = QtWidgets.QWidget(MainWindow) 
     self.centralwidget.setObjectName("centralwidget") 
     self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) 
     self.gridLayout.setObjectName("gridLayout") 
     self.listView = QtWidgets.QListView(self.centralwidget) 
     self.listView.setMaximumSize(QtCore.QSize(300, 16777215)) 
     self.listView.setObjectName("listView") 
     self.gridLayout.addWidget(self.listView, 1, 0, 1, 1) 
     self.tabWidget = QtWidgets.QTabWidget(self.centralwidget) 
     self.tabWidget.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.tabWidget.setObjectName("tabWidget") 
     self.tab = QtWidgets.QWidget() 
     self.tab.setObjectName("tab") 
     self.gridLayout_2 = QtWidgets.QGridLayout(self.tab) 
     self.gridLayout_2.setContentsMargins(0, 0, 0, 0) 
     self.gridLayout_2.setObjectName("gridLayout_2") 
     self.listWidget_2 = QtWidgets.QListWidget(self.tab) 
     self.listWidget_2.setFrameShape(QtWidgets.QFrame.NoFrame) 
     self.listWidget_2.setFrameShadow(QtWidgets.QFrame.Plain) 
     self.listWidget_2.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) 
     self.listWidget_2.setIconSize(QtCore.QSize(100, 100)) 
     self.listWidget_2.setViewMode(QtWidgets.QListView.IconMode) 
     self.listWidget_2.setModelColumn(0) 
     self.listWidget_2.setObjectName("listWidget_2") 
     self.gridLayout_2.addWidget(self.listWidget_2, 0, 0, 1, 1) 
     self.tabWidget.addTab(self.tab, "") 
     self.tab_2 = QtWidgets.QWidget() 
     self.tab_2.setObjectName("tab_2") 
     self.gridLayout_3 = QtWidgets.QGridLayout(self.tab_2) 
     self.gridLayout_3.setContentsMargins(0, 0, 0, 0) 
     self.gridLayout_3.setObjectName("gridLayout_3") 
     self.listWidget = QtWidgets.QListWidget(self.tab_2) 
     self.listWidget.setViewMode(QtWidgets.QListView.IconMode) 
     self.listWidget.setObjectName("listWidget") 
     self.gridLayout_3.addWidget(self.listWidget, 0, 0, 1, 1) 
     self.tabWidget.addTab(self.tab_2, "") 
     self.gridLayout.addWidget(self.tabWidget, 1, 1, 1, 1) 
     self.verticalLayout = QtWidgets.QVBoxLayout() 
     self.verticalLayout.setObjectName("verticalLayout") 
     self.label_2 = QtWidgets.QLabel(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth()) 
     self.label_2.setSizePolicy(sizePolicy) 
     self.label_2.setMinimumSize(QtCore.QSize(300, 130)) 
     self.label_2.setMaximumSize(QtCore.QSize(300, 100)) 
     self.label_2.setText("") 
     self.label_2.setObjectName("label_2") 
     self.verticalLayout.addWidget(self.label_2) 
     self.horizontalSlider_2 = QtWidgets.QSlider(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.horizontalSlider_2.sizePolicy().hasHeightForWidth()) 
     self.horizontalSlider_2.setSizePolicy(sizePolicy) 
     self.horizontalSlider_2.setMinimumSize(QtCore.QSize(300, 40)) 
     self.horizontalSlider_2.setMaximumSize(QtCore.QSize(300, 16777215)) 
     self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal) 
     self.horizontalSlider_2.setObjectName("horizontalSlider_2") 
     self.verticalLayout.addWidget(self.horizontalSlider_2) 
     self.horizontalLayout = QtWidgets.QHBoxLayout() 
     self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetMinimumSize) 
     self.horizontalLayout.setSpacing(0) 
     self.horizontalLayout.setObjectName("horizontalLayout") 
     self.pushButton = QtWidgets.QPushButton(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth()) 
     self.pushButton.setSizePolicy(sizePolicy) 
     self.pushButton.setMinimumSize(QtCore.QSize(50, 30)) 
     self.pushButton.setMaximumSize(QtCore.QSize(50, 30)) 
     self.pushButton.setBaseSize(QtCore.QSize(50, 30)) 
     self.pushButton.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton.setText("") 
     self.pushButton.setIconSize(QtCore.QSize(30, 30)) 
     self.pushButton.setObjectName("pushButton") 
     self.horizontalLayout.addWidget(self.pushButton) 
     self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth()) 
     self.pushButton_2.setSizePolicy(sizePolicy) 
     self.pushButton_2.setMinimumSize(QtCore.QSize(50, 30)) 
     self.pushButton_2.setMaximumSize(QtCore.QSize(50, 30)) 
     self.pushButton_2.setBaseSize(QtCore.QSize(50, 30)) 
     self.pushButton_2.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton_2.setText("") 
     self.pushButton_2.setIconSize(QtCore.QSize(30, 30)) 
     self.pushButton_2.setObjectName("pushButton_2") 
     self.horizontalLayout.addWidget(self.pushButton_2) 
     self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth()) 
     self.pushButton_3.setSizePolicy(sizePolicy) 
     self.pushButton_3.setMinimumSize(QtCore.QSize(50, 30)) 
     self.pushButton_3.setMaximumSize(QtCore.QSize(50, 30)) 
     self.pushButton_3.setBaseSize(QtCore.QSize(50, 30)) 
     self.pushButton_3.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton_3.setText("") 
     self.pushButton_3.setIconSize(QtCore.QSize(30, 30)) 
     self.pushButton_3.setObjectName("pushButton_3") 
     self.horizontalLayout.addWidget(self.pushButton_3) 
     self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.pushButton_4.sizePolicy().hasHeightForWidth()) 
     self.pushButton_4.setSizePolicy(sizePolicy) 
     self.pushButton_4.setMinimumSize(QtCore.QSize(50, 30)) 
     self.pushButton_4.setMaximumSize(QtCore.QSize(50, 30)) 
     self.pushButton_4.setBaseSize(QtCore.QSize(50, 30)) 
     self.pushButton_4.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton_4.setText("") 
     self.pushButton_4.setIconSize(QtCore.QSize(30, 30)) 
     self.pushButton_4.setObjectName("pushButton_4") 
     self.horizontalLayout.addWidget(self.pushButton_4) 
     self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.pushButton_5.sizePolicy().hasHeightForWidth()) 
     self.pushButton_5.setSizePolicy(sizePolicy) 
     self.pushButton_5.setMinimumSize(QtCore.QSize(50, 30)) 
     self.pushButton_5.setMaximumSize(QtCore.QSize(50, 30)) 
     self.pushButton_5.setBaseSize(QtCore.QSize(50, 30)) 
     self.pushButton_5.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton_5.setText("") 
     self.pushButton_5.setIconSize(QtCore.QSize(30, 30)) 
     self.pushButton_5.setObjectName("pushButton_5") 
     self.horizontalLayout.addWidget(self.pushButton_5) 
     self.verticalLayout.addLayout(self.horizontalLayout) 
     self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1) 
     self.verticalLayout_2 = QtWidgets.QVBoxLayout() 
     self.verticalLayout_2.setObjectName("verticalLayout_2") 
     self.horizontalLayout_3 = QtWidgets.QHBoxLayout() 
     self.horizontalLayout_3.setObjectName("horizontalLayout_3") 
     self.label = QtWidgets.QLabel(self.centralwidget) 
     self.label.setText("") 
     self.label.setObjectName("label") 
     self.horizontalLayout_3.addWidget(self.label) 
     self.label_3 = QtWidgets.QLabel(self.centralwidget) 
     sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(0) 
     sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth()) 
     self.label_3.setSizePolicy(sizePolicy) 
     self.label_3.setMinimumSize(QtCore.QSize(180, 180)) 
     self.label_3.setMaximumSize(QtCore.QSize(180, 180)) 
     self.label_3.setText("") 
     self.label_3.setObjectName("label_3") 
     self.horizontalLayout_3.addWidget(self.label_3) 
     self.verticalLayout_2.addLayout(self.horizontalLayout_3) 
     self.horizontalSlider = QtWidgets.QSlider(self.centralwidget) 
     self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal) 
     self.horizontalSlider.setObjectName("horizontalSlider") 
     self.verticalLayout_2.addWidget(self.horizontalSlider) 
     self.gridLayout.addLayout(self.verticalLayout_2, 0, 1, 1, 1) 
     self.horizontalSlider_3 = QtWidgets.QSlider(self.centralwidget) 
     self.horizontalSlider_3.setOrientation(QtCore.Qt.Horizontal) 
     self.horizontalSlider_3.setObjectName("horizontalSlider_3") 
     self.gridLayout.addWidget(self.horizontalSlider_3, 2, 1, 1, 1) 
     self.horizontalLayout_2 = QtWidgets.QHBoxLayout() 
     self.horizontalLayout_2.setObjectName("horizontalLayout_2") 
     self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget) 
     self.pushButton_6.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton_6.setText("") 
     self.pushButton_6.setObjectName("pushButton_6") 
     self.horizontalLayout_2.addWidget(self.pushButton_6) 
     self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget) 
     self.pushButton_7.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton_7.setText("") 
     self.pushButton_7.setObjectName("pushButton_7") 
     self.horizontalLayout_2.addWidget(self.pushButton_7) 
     self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget) 
     self.pushButton_8.setFocusPolicy(QtCore.Qt.NoFocus) 
     self.pushButton_8.setText("") 
     self.pushButton_8.setObjectName("pushButton_8") 
     self.horizontalLayout_2.addWidget(self.pushButton_8) 
     self.gridLayout.addLayout(self.horizontalLayout_2, 2, 0, 1, 1) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtWidgets.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 1155, 19)) 
     self.menubar.setObjectName("menubar") 
     self.menuFile = QtWidgets.QMenu(self.menubar) 
     self.menuFile.setObjectName("menuFile") 
     self.menuSettings = QtWidgets.QMenu(self.menubar) 
     self.menuSettings.setObjectName("menuSettings") 
     self.menuLibrary = QtWidgets.QMenu(self.menuSettings) 
     self.menuLibrary.setObjectName("menuLibrary") 
     self.menuAbout = QtWidgets.QMenu(self.menubar) 
     self.menuAbout.setObjectName("menuAbout") 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtWidgets.QStatusBar(MainWindow) 
     self.statusbar.setObjectName("statusbar") 
     MainWindow.setStatusBar(self.statusbar) 
     self.actionOpen = QtWidgets.QAction(MainWindow) 
     self.actionOpen.setObjectName("actionOpen") 
     self.actionOpen_Dir = QtWidgets.QAction(MainWindow) 
     self.actionOpen_Dir.setObjectName("actionOpen_Dir") 
     self.actionMinimize = QtWidgets.QAction(MainWindow) 
     self.actionMinimize.setObjectName("actionMinimize") 
     self.actionClose = QtWidgets.QAction(MainWindow) 
     self.actionClose.setObjectName("actionClose") 
     self.actionGeneral = QtWidgets.QAction(MainWindow) 
     self.actionGeneral.setObjectName("actionGeneral") 
     self.actionRefresh = QtWidgets.QAction(MainWindow) 
     self.actionRefresh.setObjectName("actionRefresh") 
     self.actionRebuild = QtWidgets.QAction(MainWindow) 
     self.actionRebuild.setObjectName("actionRebuild") 
     self.actionClear = QtWidgets.QAction(MainWindow) 
     self.actionClear.setObjectName("actionClear") 
     self.actionSettings = QtWidgets.QAction(MainWindow) 
     self.actionSettings.setObjectName("actionSettings") 
     self.actionAbout_Siren = QtWidgets.QAction(MainWindow) 
     self.actionAbout_Siren.setObjectName("actionAbout_Siren") 
     self.actionCheck_for_updates = QtWidgets.QAction(MainWindow) 
     self.actionCheck_for_updates.setObjectName("actionCheck_for_updates") 
     self.menuFile.addAction(self.actionOpen) 
     self.menuFile.addAction(self.actionOpen_Dir) 
     self.menuFile.addAction(self.actionMinimize) 
     self.menuFile.addAction(self.actionClose) 
     self.menuLibrary.addAction(self.actionRefresh) 
     self.menuLibrary.addAction(self.actionRebuild) 
     self.menuLibrary.addAction(self.actionClear) 
     self.menuLibrary.addAction(self.actionSettings) 
     self.menuSettings.addAction(self.menuLibrary.menuAction()) 
     self.menuSettings.addAction(self.actionGeneral) 
     self.menuAbout.addAction(self.actionAbout_Siren) 
     self.menuAbout.addAction(self.actionCheck_for_updates) 
     self.menubar.addAction(self.menuFile.menuAction()) 
     self.menubar.addAction(self.menuSettings.menuAction()) 
     self.menubar.addAction(self.menuAbout.menuAction()) 

     self.retranslateUi(MainWindow) 
     self.tabWidget.setCurrentIndex(0) 
     self.horizontalSlider_2.valueChanged['int'].connect(self.label_2.setNum) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    def retranslateUi(self, MainWindow): 
     _translate = QtCore.QCoreApplication.translate 
     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) 
     self.listWidget_2.setSortingEnabled(True) 
     self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Artist")) 
     self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Album")) 
     self.menuFile.setTitle(_translate("MainWindow", "File")) 
     self.menuSettings.setTitle(_translate("MainWindow", "Settings")) 
     self.menuLibrary.setTitle(_translate("MainWindow", "Library")) 
     self.menuAbout.setTitle(_translate("MainWindow", "About")) 
     self.actionOpen.setText(_translate("MainWindow", "Open")) 
     self.actionOpen_Dir.setText(_translate("MainWindow", "Open Dir...")) 
     self.actionMinimize.setText(_translate("MainWindow", "Minimize")) 
     self.actionClose.setText(_translate("MainWindow", "Close")) 
     self.actionGeneral.setText(_translate("MainWindow", "General")) 
     self.actionRefresh.setText(_translate("MainWindow", "Refresh")) 
     self.actionRebuild.setText(_translate("MainWindow", "Rebuild")) 
     self.actionClear.setText(_translate("MainWindow", "Clear")) 
     self.actionSettings.setText(_translate("MainWindow", "Settings")) 
     self.actionAbout_Siren.setText(_translate("MainWindow", "About Siren")) 
     self.actionCheck_for_updates.setText(_translate("MainWindow", "Check for updates")) 

當我運行class mainpanelManager我得到這個錯誤:

QtGui.QGuiApplication.__init__(self) 
TypeError: QGuiApplication(List[str]): not enough arguments 

請給我一些建議如何從運行的UI類另一個班級。 我在做什麼錯?我發現的樣本使用的是import PyQt4 import QtCore, QtGui, QtWidgets,但在我的情況下(使用Qt5)沒有QApplication選項,如Qt4。似乎Qt5使用QtGui.QGuiApplication。我無法理解這一點。任何幫助將受到歡迎。請詳細解釋。我在Python中仍然很新。

+0

那裏有很多代碼。也許你可以做一個[mcve]? –

回答

1

你需要從Qt設計頂層對象匹配的子類 - 在這種情況下,似乎是一個QMainWindow

class mainpanelManager(QtWidgets.QMainWindow, Ui_MainWindow): 
    def __init__(self): 
     super().__init__() 
     self.setupUi(self) 

而且你應該使用QApplication

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv) 
    window = mainpanelManager() 
    window.show() 
    sys.exit(app.exec_()) 

PS:

PyQt4與PyQt5不兼容。請參閱:Differences Between PyQt4 and PyQt5

PPS:

它也像你嘗試編輯由pyuic生成的模塊。永遠不要這樣做。我建議你重新運行pyuic,以便獲得一個乾淨的模塊。