简体   繁体   中英

How to place toolbar - PyQt4

How do I place toolbar at the right/left side of the screen. I tried to use:

self.formatbar = self.addToolBar("Format") self.formatbar.setAllowedAreas(Qt.RightToolBarArea)

I am not getting any errors but the toolbar is still at the top.

setAllowedAreas defines the area the toolbar is allowed to be when it's being dragged around by the user (the default is Qt.AllToolBarAreas so you may still want to change this). To actually place it at a specific side, you need to specify it when you add it. For example:

self.formatbar = QToolBar() 
self.addToolBar( Qt.LeftToolBarArea , self.formatbar )

I was searching for help for this same issue and came across two Stack Overflow questions on the topic (which includes this one).

As I was testing out the suggested code, I stumbled on what seems to be a suitable solution.

tool_bar.setStyleSheet("font-family: Arial; font-size: 12pt; border-top: none; margin-top: 10px;")

You may examine my code to get a better understanding of what I was trying to achieve. I placed headers between the MenuBar and ToolBar lines of code help locate the line(s) you wish to see.

from PyQt5.QtWidgets import *
from PyQt5 import QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


font = QFont("Arial", 12)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt5 | Positioning the ToolBar")
        self.setWindowIcon(QtGui.QIcon("IMAGES II/PyQt5_Icon.ico"))
        self.setFont(font)
        self.setStyleSheet("background-color: #000000")
        self.resize(1250, 750)
        self.gui()

    def gui(self):

        self.widgets()
        self.center_window()
        self.show()

    def widgets(self):

        # ==================== Creating the MenuBar ====================

        menu_bar = self.menuBar()
        menu_bar.setStyleSheet("font-size: 12pt; color: #716E6E")

        # ==================== Create the MenuBar Items ====================

        file_menu = menu_bar.addMenu("File")
        edit_menu = menu_bar.addMenu("Edit")
        view_menu = menu_bar.addMenu("View")
        synchronize_menu = menu_bar.addMenu("Synchronize")
        window_menu = menu_bar.addMenu("Window")
        help_menu = menu_bar.addMenu("Help")

        # ==================== Create the File Menu's Submenus and Menu Items ====================

        # Create the Submenu(s):
        file_submenu = QMenu("New", self)
        file_submenu.setStyleSheet("font-size: 12pt; color: #716E6E")

        file_properties_submenu = QMenu("File Properties", self)
        file_properties_submenu.setStyleSheet("font-size: 12pt; color: #716E6E")

        # This is a submenu within the file_properties_submenu
        line_separators = QMenu("Line Separators", self)
        line_separators.setStyleSheet("font-size: 12pt; color: #716E6E")

        # Create file_submenu Items:
        new_file = QAction("New File", self)
        directory = QAction("Directory", self)
        python_file = QAction("Python File", self)
        html_file = QAction("HTML File", self)
        open = QAction("Open", self)
        save_as = QAction("Save as", self)
        settings = QAction("Settings", self)
        exit = QAction("Exit", self)
        exit.triggered.connect(self.close_app)

        # Add Items to file_submenu Submenu:
        file_submenu.addAction(new_file)
        file_submenu.addAction(directory)
        file_submenu.addSeparator()
        file_submenu.addAction(python_file)
        file_submenu.addAction(html_file)

        # Create file_properties_submenu Items:
        file_encoding = QAction("File Encoding", self)
        remove_bom = QAction("Remove BOM", self)
        add_bom = QAction("Add BOM", self)
        associate_with_file_type = QAction("Associate with File Type", self)

        # Create line_separators_submenu Items:
        crlf_windows = QAction("CRLF - Windows", self)
        lf_unix_and_macos = QAction("LF - Unix and macos", self)
        cs_classic_macos = QAction("CS - Classic macos", self)

        # Add Items to line_separatos submenu:
        line_separators.addAction(crlf_windows)
        line_separators.addAction(lf_unix_and_macos)
        line_separators.addAction(cs_classic_macos)

        # Add Items and Submenus to file_properties_submenu:
        file_properties_submenu.addAction(file_encoding)
        file_properties_submenu.addAction(remove_bom)
        file_properties_submenu.addAction(add_bom)
        file_properties_submenu.addSeparator()
        file_properties_submenu.addAction(associate_with_file_type)
        file_properties_submenu.addMenu(line_separators)

        # Add Submenus and Menu Items to File Menu:
        file_menu.addMenu(file_submenu)
        file_menu.addAction(open)
        file_menu.addAction(save_as)
        file_menu.addSeparator()
        file_menu.addAction(settings)
        file_menu.addMenu(file_properties_submenu)
        file_menu.addSeparator()
        file_menu.addAction(exit)

        # ==================== Edit Menu Submenus and Menu Items ====================

        # Create Menu Items:
        undo_ = QAction("Undo", self)
        redo_ = QAction("Redo", self)
        cut_ = QAction("Cut", self)
        copy_ = QAction("Copy", self)
        paste_ = QAction("Paste", self)

        # Add Menu Items to Edit Menu:
        edit_menu.addAction(undo_)
        edit_menu.addAction(redo_)
        edit_menu.addSeparator()
        edit_menu.addAction(cut_)
        edit_menu.addAction(copy_)
        edit_menu.addAction(paste_)


        # ==================== Creating a Vertical ToolBar ====================

        # Create the ToolBar
        tool_bar = self.addToolBar("")

        # Position the ToolBar on the left side of the Main Window
        self.addToolBar(Qt.LeftToolBarArea, tool_bar)

        # Prevent the User from moving the ToolBar
        tool_bar.setMovable(False)

        # Enable descriptive text to be displayed under the
        # ToolButton Icons
        tool_bar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        # Set font family, font size, and color of text for ToolBar Buttons
        tool_bar.setStyleSheet("font-family: Arial; font-size: 10pt; border-top: none; margin-top: 10px")

        # Set the size of the ToolButton Icons
        tool_bar.setIconSize(QSize(35, 35))

        # Create ToolBar item-icons with text
        python_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/python.png"), "Python", self)
        python_tool_bar_btn.setToolTip("Python Programming")

        c_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/c-file.png"), "C", self)
        c_tool_bar_btn.setToolTip("C Programming")

        java_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/java.png"), "Java", self)
        java_tool_bar_btn.setToolTip("Java Programming")

        javascript_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/javascript.png"), "Javascript", self)
        javascript_tool_bar_btn.setToolTip("Javascript Programming")

        swift_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/swift.png"), "Swift", self)
        swift_tool_bar_btn.setToolTip("Swift Programming")

        stack_overflow_tool_bar_btn = QAction(QtGui.QIcon("IMAGES I/stack-overflow.png"), "Stack\nOverflow", self)
        stack_overflow_tool_bar_btn.setToolTip("Stack Overflow")

        # Add item-icons to ToolBar
        tool_bar.addAction(python_tool_bar_btn)
        tool_bar.addAction(c_tool_bar_btn)
        tool_bar.addAction(java_tool_bar_btn)
        tool_bar.addAction(javascript_tool_bar_btn)
        tool_bar.addAction(swift_tool_bar_btn)
        tool_bar.addAction(stack_overflow_tool_bar_btn)

        # Define a single function that provides functionality
        # to all the ToolBar item-icons
        tool_bar.actionTriggered.connect(self.tool_bar_actions)

    def tool_bar_actions(self, tool_bar_btn):
        if tool_bar_btn.text() == "Python":
            print("Python and PyQt5 GUI Programming")

        elif tool_bar_btn.text() == "C":
            print("C Programming")

        elif tool_bar_btn.text() == "Java":
            print("Java Programming")

        elif tool_bar_btn.text() == "Javascript":
            print("Javascript Programming")

        elif tool_bar_btn.text() == "Swift":
            print("Swift Programming")

        else:
            print("Stack Overflow Developer's Community")

    def close_app(self):
        self.close()

    def center_window(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


def main():
    App = QApplication(sys.argv)

    App.setStyleSheet(
        """
        
        QMenuBar { background-color: #000000; font-family: Arial; font-size: 12pt; color: #0476B3; }
        
        QMenuBar::item:selected { background-color: #c8e5ff; color: #0476B3; }
        
        QMenu::item:selected { background-color: #c8e5ff; color: #0476B3; }
        
        """
    )

    main_window = MainWindow()
    sys.exit(App.exec_())


if __name__ == '__main__':
    main()

Example ToolBar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM