0

In my GUI design, I want to seperate menuBar(), statusBar(), addToolBar() etc. The problem I'm facing is not to know how to reach open_file variable of menuBar() function for addToolBar().

I know that If I define open_file as self.open_file, I can reach it where ever I want. However, I'm tring to figure out alternative way without using self. for each variable for further coding.

When I search that, I found this link But, I think that calling variables with this method may cause confusion or noise in the GUI design.

Here is my code,

import sys

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


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

        self.show()

    def init_ui(self):
        title = "General Body"
        top = 100
        left = 350
        width = 600
        height = 500
        self.setWindowTitle(title)
        self.setGeometry(left, top, width, height)

        self.menu_bar()
        self.status_bar()
        self.tool_bars()

    def menu_bar(self):
        bar_menu = self.menuBar()
        file_menu = bar_menu.addMenu("File")
        # viewMenu = bar.addMenu("View")
        # editMenu = bar.addMenu("Edit")
        # searchMenu = bar.addMenu("Search")
        # toolMenu = bar.addMenu("Tool")
        # helpMenu = bar.addMenu("Help")

        # File Menu =========================
        open_file = QAction("Open", self)
        open_file.setShortcut("Ctrl+O")
        open_file.setStatusTip("Open File")

        save_file = QAction("Save", self)
        save_file.setShortcut("Ctrl+S")
        save_file.setStatusTip("Save File")

        exit_file = QAction("Exit", self)
        exit_file.setShortcut("Ctrl+Q")
        exit_file.setStatusTip("Close Program")
        exit_file.triggered.connect(self.exit_file_func)

        file_menu.addAction(open_file)
        file_menu.addAction(save_file)
        file_menu.addAction(exit_file)

    def status_bar(self):
        bar_status = self.statusBar()
        bar_status.showMessage("Read!")

    def tool_bars(self):
        tool_bar1 = self.addToolBar("Operations")
        tool_bar1.addAction(open_file)

    @staticmethod
    def exit_file_func():
        msg_exit = QMessageBox()
        msg_exit.setWindowTitle("Warning!")
        msg_exit.setText("Would you like to exit ?")
        msg_exit.setIcon(QMessageBox.Question)
        msg_exit.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg_exit.setDefaultButton(QMessageBox.No)
        x = msg_exit.exec()
        if x == msg_exit.Yes:
            QApplication.quit()


app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec())

1 Answer 1

1

I think, I overcome the problem.

Here is part of my code;

def menu_bar(self):
    bar_menu = self.menuBar()
    file_menu = bar_menu.addMenu("File")

    # File Menu =========================

    open_file = QAction("Open", self)
    open_file.setShortcut("Ctrl+O")
    open_file.setStatusTip("Open File")

    save_file = QAction("Save", self)
    save_file.setShortcut("Ctrl+S")
    save_file.setStatusTip("Save File")

    return open_file, save_file
def tool_bars(self):
    a, b = self.menu_bar()
    tool_bar1 = self.addToolBar("Operations")
    tool_bar1.addAction(a)
    tool_bar1.addAction(b)

In this way, there looks no problem. However, your feedback about the way i did, is still on table

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.