Reputation: 727
I've created a class, which creates the GUI. I would like to add a menubar to it, but I don't really know, how should I add it to the window, if I work with a class. I can't make the menu bar appaer.
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
Main = QtGui.QMainWindow()
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.tempLabel=QtGui.QLabel("NC",self)
self.tempLabel.move(350,20)
self.tempLabel.setStyleSheet('color: black; font-size: 12pt;font: bold')
#menu bar
self.menu=QtGui.QMenu("Port", self)
self.menu.addAction('&ttyUSB0',)
self.menu.addAction('&ttyUSB1',)
self.menu.addAction('&ttyUSB2',)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.canvas)
self.layout.addWidget(self.tempLabel)
self.tab1.setLayout(self.layout)
self.tabs = QtGui.QTabWidget()
self.tabs.addTab(self.tab1, "Database")
self.tabs.addTab(self.tab2, "Current")
self.tabs.show()
Upvotes: 0
Views: 51
Reputation: 120568
The menu bar is usually accessed from the main window, using the menuBar function.
I have edited your example code to show how to add menus, and also fixed a few other minor issues:
from PyQt4 import QtCore, QtGui
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
menubar = self.menuBar()
menu = menubar.addMenu('Port')
menu.addAction('&ttyUSB0')
menu.addAction('&ttyUSB1')
menu.addAction('&ttyUSB2')
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.tempLabel = QtGui.QLabel('NC', self)
self.tempLabel.move(350, 20)
self.tempLabel.setStyleSheet(
'color: black; font-size: 12pt;font: bold')
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.canvas)
self.layout.addWidget(self.tempLabel)
self.tab1.setLayout(self.layout)
self.tabs = QtGui.QTabWidget()
self.tabs.addTab(self.tab1, 'Database')
self.tabs.addTab(self.tab2, 'Current')
self.setCentralWidget(self.tabs)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Upvotes: 1