PyQt5 中的 menuBar() [英] menuBar() in PyQt5

查看:110
本文介绍了PyQt5 中的 menuBar()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将菜单栏加载到我的 gui 上,但是我的类对象没有 self.menuBar() 的属性.有人可以帮助我吗,似乎没有教程提供任何解决方法.

class EmailBlast(QtWidgets.QWidget):def __init__(self):super().__init__()bar = QtWidgets.menuBar()file_menu = bar.addMenu('文件')file_edit = bar.addMenu('编辑')

错误信息:

文件BasicEmail.py",第 84 行,在 email_config 中self.ui = EmailBlast()文件BasicEmail.py",第 96 行,在 __init__ 中self.menuBar()AttributeError: 'EmailBlast' 对象没有属性 'menuBar'

我在这里遗漏了什么.

更新项目:

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_edit = bar.addMenu('编辑')类 EmailBlast(QtWidgets.QWidget):def __init__(self):super().__init__()self.text_box = QtWidgets.QTextEdit(self)self.save_button = QtWidgets.QPushButton('保存')self.clear_button = QtWidgets.QPushButton('Clear')self.open_button = QtWidgets.QPushButton('打开')self.init_ui()

解决方案

Exception 说实话:QWidget 类没有 menuBar 属性,

如果您在代码中的某处有 QMainWindow 实例并且只想在 EmailBlast init 中填充菜单项,您可以获得提到的 QMainWindow 的菜单栏code> 通过调用 bar = QtWidgets.QMainWindow.menuBar().菜单由 QAction 组成,因此您可能也需要添加它们.

如果您没有 QMainWindow,请考虑添加一个.QMainWindow 必须有Central Widget".在您的情况下,它可能是 EmailBlast 小部件.由于 EmailBlast 将成为 MainWindow 的一部分,您需要创建和显示 MainWindow 实例而不是 EmailBlast.>

如果您在 EmailBlast 小部件中有多个元素(按钮、文本编辑等),那么 Qt

I am trying to load a menubar onto my gui, but my class object has no attribute for self.menuBar(). Can someone help me, no tutorials seem to offer any 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 am I 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 does not have a menuBar attribute, QMainWindow has (as well as toolbars and status bar):

If you have QMainWindow instance somewhere in your code and just want to fill menu items in EmailBlast init, you can get menu bar of mentioned QMainWindow by calling bar = QtWidgets.QMainWindow.menuBar(). Menus consists of an QActions so you probably need to add them too.

If you do not have QMainWindow then consider adding one. QMainWindow must have "Central Widget". In your case it is probably EmailBlast widget. Since EmailBlast will be a part of MainWindow you need to create and show MainWindow instance instead of EmailBlast.

If you have multiple elements (buttons, text edits etc.) in EmailBlast widget then Qt layout system comes into play almost inevitable (quickly glance at pics in docs to grasp a concept).

It can be pretty hard to wrap a head around all this things at once, so once again.

QMainWindow - central part of your app. Has menu bar, toolbars, status bar and central area occupied by central widget.

Central widget - a widget providing main functionality of your app (or it can be a widget holding other widgets like QSplitter). In your case it is probably a EmailBlast widget.

EmailBlast widget provides a piece of (reusable) functionality. To do so it itself consists of various helper widgets (text edits, buttons, check boxes and so on). To place this small widgets in a predictable manner the layout is created. Widgets are placed inside layout and layout is set to EmailBlast widget.

Menu bar consists of zero or more QMenus, which in turn can have QActions. QAction signals (usually triggered) are connecting to slots to provide desired behavior.

Here is a complete example:

import sys
from PyQt5 import QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        # creating EmailBlast widget and setting it as central
        self.email_blast_widget = EmailBlast(parent=self)
        self.setCentralWidget(self.email_blast_widget)
        # filling up a menu bar
        bar = self.menuBar()
        # File menu
        file_menu = bar.addMenu('File')
        # adding actions to 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 to 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 to bind signals to desired behavior
        close_action.triggered.connect(self.close)


class EmailBlast(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        # create and set layout to 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 to 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 like that:

这篇关于PyQt5 中的 menuBar()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆