I run the following sample code that comes with the PySide2 ebook
import sys
from PySide2.QtCore import QSize, Qt
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QToolBar,
QAction,
QStatusBar,
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
label = QLabel("Hello!")
label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(label)
toolbar = QToolBar("My main toolbar")
toolbar.setIconSize(QSize(16, 16))
# I also tried this, but it still doesn't work
toolbar.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.addToolBar(toolbar)
# @@@ Attach icon to QAction
button_action = QAction(QIcon("bug.png"), "Your button", self)
button_action.setStatusTip("This is your button")
button_action.triggered.connect(self.onMyToolBarButtonClick)
button_action.setCheckable(True)
toolbar.addAction(button_action)
self.setStatusBar(QStatusBar(self))
def onMyToolBarButtonClick(self, s):
print("click", s)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
The bug.png
image is sitting next to the source code toolbars_and_menus_5.py
. But for some reason my GUI output doesn’t show any icon at all.
Any idea what went wrong?
I am running on macOS Catalina (v10.15.6) + Python 3.7 + pip install PySide2.
I also tried on Win10 Pro + Python 3.7 + pip install PySide2 .
Same problem. The icon is still missing.
Thanks