微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在QMainWindow PyQt5中显示小部件

如何解决在QMainWindow PyQt5中显示小部件

我正在尝试使用PyQt5制作GUI。我在窗口中显示小部件时遇到问题。

我的主窗口类继承了QMainWindow基类的功能

    class App(QtWidgets.QMainWindow): # This class inherits functions from QMainWindow base class

        def __init__(self):
            super().__init__()
    
            self.initUI() # Call initUI function that initilize app window
    
            
        def initUI(self):
    
            # Set window geometry and title
            self.title = 'This is a title'
            self.left = 0
            self.top = 50
            self.width = 1024
            self.height = 800
    
            self.setGeometry(self.left,self.top,self.width,self.height)
            self.setwindowTitle(self.title)
    
            # Add icon to Window
            self.setwindowIcon(QtGui.QIcon('icon.bmp'))
    
            self.CreateMenu()
    
            self.mywidgets=self.CreateLayout() 
            self.setCentralWidget(self.mywidgets)

            self.show()

我将主窗口类中的菜单栏和工具栏定义为:

    def CreateMenu(self):
   #%% ***************CREATE MENU************************************************************
   
            mainMenu = self.menuBar()
            self.fileMenu = mainMenu.addMenu('File')
            self.editMenu = mainMenu.addMenu('Edit')
            self.viewMenu = mainMenu.addMenu('View')
            self.toolsMenu = mainMenu.addMenu('Tools')
            self.helpMenu = mainMenu.addMenu('Help')
    
            self.newAction = QAction(QIcon("New.png"),'New file',self)
            self.newAction.setShortcut("Ctrl+N")
            self.fileMenu.addAction(self.newAction)
    
            self.openAction = QAction(QIcon("Open.png"),'Open',self)
            self.openAction.setShortcut("Ctrl+O")
            self.fileMenu.addAction(self.openAction)
    
            self.saveAction = QAction(QIcon("Save.png"),'Save',self)
            self.saveAction.setShortcut("Ctrl+S")
            self.fileMenu.addAction(self.saveAction)

    
            self.helpAction = QAction(QIcon("Help.png"),'Help',self)
            self.helpAction.setShortcut("Ctrl+H")
            self.helpMenu.addAction(self.helpAction)
    
            self.exitaction = QAction(QIcon("exit.png"),'Exit',self)
            self.exitaction.setShortcut("Ctrl+E")
            self.fileMenu.addAction(self.exitaction)       
    
    
            # Create toolbar and add buttons to it
            self.toolbar = self.addToolBar('Toolbar')
            self.toolbar.addAction(self.newAction)
            self.toolbar.addAction(self.openAction)
            self.toolbar.addAction(self.saveAction)
            self.toolbar.addAction(self.exitaction)
            self.toolbar.addAction(self.helpAction)

            # Maximize window on startup    
            self.showMaximized()

我还将主窗口类中的布局和小部件定义为:

    def CreateLayout(self):

            # Create main vertical Box layout and set it
            self.layout = QtWidgets.QVBoxLayout(self)       
            self.setLayout(self.layout)
            
            # Initialize tab screen
            self.tabs = QtWidgets.QTabWidget()
            self.tab1 = QtWidgets.QWidget() # Raw data plotting tab
            self.tab2 = QtWidgets.QWidget() # Data analysis tab


           #%% **************CREATE TABS*************************************************************
            # Initialize tab screen
            self.tabs = QtWidgets.QTabWidget()
            self.tab1 = QtWidgets.QWidget()    # Tab for plotting raw data
            self.tab2 = QtWidgets.QWidget()    # Data analysis tab


            # Add tabs
            self.tabs.addTab(self.tab1,"Tab1")
            self.tabs.addTab(self.tab2,"Tab2")

            #===========Create first tab==============================================
            self.tab1.layout = qgridLayout(self)
    #------------------------------------------------------------------------------------------------
            # Create frame for sensors
            self.SensorGroupBox = qgroupbox("Sensors")
            self.SensorGroupBox.setFont(QtGui.QFont("Sanserif",15))
            self.tab1.layout.addWidget(self.SensorGroupBox,2,2) # add frame to main layout and define span

            # Create frame for options
            self.PlotGroupBox = qgroupbox("Plot options")
            self.PlotGroupBox.setFont(QtGui.QFont("Sanserif",15))
            self.tab1.layout.addWidget(self.PlotGroupBox,2) # add frame to main layout and define span
    
    #------------------------------------------------------------------------------------------------
            self.vBox = QVBoxLayout() # Create vertical layout to place items inside the Sensor frame
    
            # Define tree widget for all sensors     
            tree    = QtWidgets.QTreeWidget()
            tree.setHeaderLabels(['Sensor','Time'])
            self.vBox.addWidget(tree,0)
            tree.header().setStretchLastSection(True);
    
            for i in range(3):
                parent = QtWidgets.QTreeWidgetItem(tree)
                parent.setText(0,"Parent {}".format(i))
                parent.setFlags(parent.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
                for x in range(5):
                    child = QtWidgets.QTreeWidgetItem(parent)
                    child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
                    child.setText(0,"Child {}".format(x))
                    child.setCheckState(0,Qt.Unchecked)
             tree.show() 
    
            self.SensorGroupBox.setLayout(self.vBox) # Set vertical layout to the frame
    #------------------------------------------------------------------------------------------------
            # Graph Widget
            self.graphWidget = pg.PlotWidget()
            self.graphWidget.setFrameShape(QtWidgets.qframe.Box)
            self.graphWidget.getPlotItem().showGrid(x=True,y=True,alpha=0.9)
            self.graphWidget.setBackground('k')
            x=[]
            y=[]
            self.plot = self.graphWidget.plot(x,y,pen=pg.mkPen('r',alpha=0.85))
            self.tab1.layout.addWidget(self.graphWidget,4,1)

            # Set tab1 layout
            self.tab1.setLayout(self.tab1.layout)

            # Add tabs to widget
            self.layout.addWidget(self.tabs)
            self.setLayout(self.layout)

运行代码时,我的菜单栏和工具栏将显示并正常工作,但是我的小部件均未显示。我认为问题可能出在我设置中央小部件的位置。我不确定这是否正确:

    self.mywidgets=self.CreateLayout() 
    self.setCentralWidget(self.mywidgets)

有人可以告诉我我在做什么错吗?

注意:我知道我可以为我的小部件定义单独的类:

    class MyWidget(QtWidgets.QWidget): # This class inherits functions from QWidget base class
    
        def __init__(self,parent):
            super(QtWidgets.QWidget,self).__init__(parent)

        .
        .
        .

然后在我的主类中创建一个实例,并将其设置为中央小部件,如下所示:

    # Run MyWidget class
    self.my_widgets = MyWidget(self)
    self.setCentralWidget(self.my_widgets)

这可行,但是我想拥有一个类(QmainWindow)并在那里定义所有内容

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。