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())