我正在尝试使用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,0,0,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,0,2,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,0,2,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)并在那里定义所有内容。