PYQT5 - Cara Membuat MenuBar , Toolbar , Context Menu , Status Bar pada QMainWindow di PyQt5
Kali ini kita akan mencoba membuat MenuBar , Toolbar , Context Menu pada pyqt5. dan yang perlu diperhatikan untuk pertamakali, ketika kita ingin membuat Menubar dan toolbar, maka widget yangharus kita gunakan adalah QMainWindow
Cara Bikin Menu Bar
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu,QMenuBar,QAction
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.resize(800, 600)
# ======= Memasang Menu Bar ========
menuBar = QMenuBar(self)
self.setMenuBar(menuBar)
# ======= Membuat Header Menu dengan QMenu ========
fileMenu = QMenu("&File", self)
menuBar.addMenu(fileMenu)
#====== Membuat Action Untuk File =========
self.newAction = QAction(self)
self.newAction.setText("&New")
self.newAction.triggered.connect(self.newActionProcess)
fileMenu.addAction(self.newAction)
self.openAction = QAction(self)
self.openAction.setText("&Open")
self.openAction.triggered.connect(self.openActionProcess)
fileMenu.addAction(self.openAction)
self.saveAction = QAction(self)
self.saveAction.setText("&Save")
self.saveAction.triggered.connect(self.saveActionProcess)
fileMenu.addAction(self.saveAction)
self.exitAction = QAction(self)
self.exitAction.setText("&Exit")
self.exitAction.triggered.connect(self.exitActionProcess)
fileMenu.addAction(self.exitAction)
# ======= Membuat Header Menu dengan Title ========
editMenu = menuBar.addMenu("&Edit")
#====== Membuat Action Untuk Edit =========
self.cutAction = QAction(self)
self.cutAction.setText("&Cut")
self.cutAction.triggered.connect(self.cutActionProcess)
editMenu.addAction(self.cutAction)
self.copyAction = QAction(self)
self.copyAction.setText("&Copy")
self.copyAction.triggered.connect(self.copyActionProcess)
editMenu.addAction(self.copyAction)
self.pasteAction = QAction(self)
self.pasteAction.setText("&Paste")
self.pasteAction.triggered.connect(self.pasteActionProcess)
editMenu.addAction(self.pasteAction)
self.show()
def newActionProcess(self):
print("newActionProcess")
def openActionProcess(self):
print("openActionProcess")
def saveActionProcess(self):
print("saveActionProcess")
def exitActionProcess(self):
print("exitActionProcess")
def cutActionProcess(self):
print("cutActionProcess")
def copyActionProcess(self):
print("copyActionProcess")
def pasteActionProcess(self):
print("pasteActionProcess")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
Cara Bikin Tool Bar
from PyQt5.QtWidgets import QApplication, QMainWindow,QAction,QToolBar
from PyQt5.QtGui import QIcon
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.resize(800, 600)
#nambah Toolbar
toolbar = QToolBar("My main toolbar")
self.addToolBar(toolbar)
#nambah Action Button degan icon
button_action = QAction(QIcon("icon.png"), "Your button", self)
#nambah Action Button tanpa icon
#button_action = QAction("Your button", self)
button_action.setStatusTip("This is your button")
button_action.setCheckable(True)
button_action.triggered.connect(self.onMyToolBarButtonClick)
toolbar.addAction(button_action)
self.show()
def onMyToolBarButtonClick(self):
print("click")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
Cara Bikin Status Bar
Berikut adalah contoh kode yang bisa kita gunakan, ketika kita menggunakan QTDesigner , dan convert dengan cara pyuic5 -x ouput.ui -o ouput.py
from PyQt5 import QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(800, 261)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.statusbar.showMessage("Hallo Apa Kabar")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Berikut adalah contoh kode jika kita menggunakan QMainWIndow
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.statusbar = QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
self.statusbar.showMessage("Hallo Apa Kabar")
self.show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
Cara Bikin Context Menu
Berikut adalah contoh kode yang bisa kita gunakan, ketika kita menggunakan QTDesigner , dan convert dengan cara pyuic5 -x ouput.ui -o ouput.py
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
MainWindow.customContextMenuRequested.connect(self.myContextMenu)
def myContextMenu(self, pos):
menu = QtWidgets.QMenu()
menu_hello = menu.addAction('Print Hello World')
menu_goodbye = menu.addAction('Print GoodBye')
menu_exit = menu.addAction('Exit')
menu_hello.triggered.connect(lambda: print('Hello World'))
menu_goodbye.triggered.connect(lambda: print('Goodbye'))
menu_exit.triggered.connect(lambda: exit())
menu.exec_(QtGui.QCursor.pos())
Berikut adalah contoh kode jika kita memanggil QMainWIndow, maka kita tinggalmembuat method contextMenuEvent
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.show()
def contextMenuEvent(self, event):
contextMenu = QMenu(self)
menu_hello = contextMenu.addAction("Print Hello")
menu_goodbye = contextMenu.addAction("print goodby")
menu_exit = contextMenu.addAction("exit")
menu_hello.triggered.connect(lambda: print('Hello World'))
menu_goodbye.triggered.connect(lambda: print('Goodbye'))
menu_exit.triggered.connect(lambda: exit())
action = contextMenu.exec_(self.mapToGlobal(event.pos()))
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())