python - menuBar() in PyQt5 -


i trying load menubar onto gui, class object has no attribute self.menubar(). can me, no tutorials seem offer way around.

class emailblast(qtwidgets.qwidget):     def __init__(self):         super().__init__()         bar = qtwidgets.menubar()          file_menu = bar.addmenu('file')         file_edit = bar.addmenu('edit')         

error message:

file "basicemail.py", line 84, in email_config self.ui = emailblast() file "basicemail.py", line 96, in __init__ self.menubar() attributeerror: 'emailblast' object has no attribute 'menubar' 

what missing here.

updated project:

class mainwindow(qtwidgets.qmainwindow): def __init__(self):     super().__init__()     self.email_blast_widget = emailblast()     self.setcentralwidget(self.email_blast_widget)     bar = self.menubar()     file_file = bar.addmenu('file')              file_edit = bar.addmenu('edit')   class emailblast(qtwidgets.qwidget): def __init__(self):     super().__init__()     self.text_box = qtwidgets.qtextedit(self)     self.save_button = qtwidgets.qpushbutton('save')     self.clear_button = qtwidgets.qpushbutton('clear')             self.open_button = qtwidgets.qpushbutton('open')             self.init_ui() 

exception speaks truth: qwidget class not have menubar attribute, qmainwindow has (as toolbars , status bar):

qmainwindow layout

if have qmainwindow instance somewhere in code , want fill menu items in emailblast init, can menu bar of mentioned qmainwindow calling bar = qtwidgets.qmainwindow.menubar(). menus consists of qactions need add them too.

if not have qmainwindow consider adding one. qmainwindow must have "central widget". in case emailblast widget. since emailblast part of mainwindow need create , show mainwindow instance instead of emailblast.

if have multiple elements (buttons, text edits etc.) in emailblast widget qt layout system comes play inevitable (quickly glance @ pics in docs grasp concept).

it can pretty hard wrap head around things @ once, once again.

qmainwindow - central part of app. has menu bar, toolbars, status bar , central area occupied central widget.

central widget - widget providing main functionality of app (or can widget holding other widgets qsplitter). in case emailblast widget.

emailblast widget provides piece of (reusable) functionality. consists of various helper widgets (text edits, buttons, check boxes , on). place small widgets in predictable manner layout created. widgets placed inside layout , layout set emailblast widget.

menu bar consists of 0 or more qmenus, in turn can have qactions. qaction signals (usually triggered) connecting slots provide desired behavior.

here complete example:

import sys pyqt5 import qtwidgets   class mainwindow(qtwidgets.qmainwindow):     def __init__(self, parent=none):         super().__init__(parent)         # creating emailblast widget , setting central         self.email_blast_widget = emailblast(parent=self)         self.setcentralwidget(self.email_blast_widget)         # filling menu bar         bar = self.menubar()         # file menu         file_menu = bar.addmenu('file')         # adding actions file menu         open_action = qtwidgets.qaction('open', self)         close_action = qtwidgets.qaction('close', self)         file_menu.addaction(open_action)         file_menu.addaction(close_action)         # edit menu         edit_menu = bar.addmenu('edit')         # adding actions edit menu         undo_action = qtwidgets.qaction('undo', self)         redo_action = qtwidgets.qaction('redo', self)         edit_menu.addaction(undo_action)         edit_menu.addaction(redo_action)          # use `connect` method bind signals desired behavior         close_action.triggered.connect(self.close)   class emailblast(qtwidgets.qwidget):     def __init__(self, parent=none):         super().__init__(parent)         # create , set layout place widgets         grid_layout = qtwidgets.qgridlayout(self)          self.text_box = qtwidgets.qtextedit(self)         self.save_button = qtwidgets.qpushbutton('save')         self.clear_button = qtwidgets.qpushbutton('clear')         self.open_button = qtwidgets.qpushbutton('open')         # add widgets layout. params are:         # (widget, fromrow, fromcolumn, rowspan=1, columnspan=1)         grid_layout.addwidget(self.text_box, 0, 0, 1, 3)         grid_layout.addwidget(self.save_button, 1, 0)         grid_layout.addwidget(self.clear_button, 1, 1)         grid_layout.addwidget(self.open_button, 1, 2)   if __name__ == '__main__':     app = qtwidgets.qapplication(sys.argv)     # creating main window     mw = mainwindow()     mw.show()     sys.exit(app.exec_()) 

this code produces nice little app that:

app window


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -