?print-pdf' Created for
self.textedit = qtw.QTextEdit()
self.setCentralWidget(self.textedit)
# get the menu bar
menubar = self.menuBar()
# add menu items
file_menu = menubar.addMenu('File')
edit_menu = menubar.addMenu('Edit')
help_menu = menubar.addMenu('Help')
# add menu items
file_menu = menubar.addMenu('&File')
edit_menu = menubar.addMenu('&Edit')
help_menu = menubar.addMenu('&Help')
toolbar = self.addToolBar('File')
toolbar.setMovable(False)
toolbar.setFloatable(False)
# for Qt6:
toolbar.setAllowedAreas(
qtc.Qt.ToolBarArea.TopToolBarArea |
qtc.Qt.ToolBarArea.BottomToolBarArea
)
# for Qt5:
# toolbar.setAllowedAreas(
# qtc.Qt.TopToolBarArea |
# qtc.Qt.BottomToolBarArea
# )
# add actions
open_action = file_menu.addAction('Open')
save_action = file_menu.addAction('Save')
# add separator
file_menu.addSeparator()
quit_action = file_menu.addAction('Quit', self.close)
undo_action = edit_menu.addAction('Undo', self.textedit.undo)
# create a QAction manually
redo_action = qtg.QAction('Redo', self)
redo_action.triggered.connect(self.textedit.redo)
# Actions, which opens custom dialog
edit_menu.addAction(redo_action)
edit_menu.addAction('Set Font…', self.set_font)
edit_menu.addAction('Settings…', self.show_settings)
self.help_action = qtg.QAction(
self.style().standardIcon(qtw.QStyle.StandardPixmap.SP_DialogHelpButton),
'Help',
self # important to pass the parent!
)
# add signal
self.help_action.triggered.connect(lambda : qtw.QMessageBox.information(self,'Not Implemented','Sorry, no help yet!'))
toolbar.addAction(help_action)
dock = qtw.QDockWidget("Replace")
self.addDockWidget(qtc.Qt.LeftDockWidgetArea, dock)
# set dock widget to move and float (but not closeable)
dock.setFeatures(
# for pyqt6
qtw.QDockWidget.DockWidgetFeature.DockWidgetMovable |
qtw.QDockWidget.DockWidgetFeature.DockWidgetFloatable
# for pyqt5
# qtw.QDockWidget.DockWidgetMovable |
# qtw.QDockWidget.DockWidgetFloatable
)
replace_widget = qtw.QWidget()
replace_widget.setLayout(qtw.QVBoxLayout())
self.search_text_input = qtw.QLineEdit()
self.search_text_input.setPlaceholderText('search')
self.replace_text_input = qtw.QLineEdit()
self.replace_text_input.setPlaceholderText('replace')
search_and_replace_btn = qtw.QPushButton(
"Search and Replace",
)
search_and_replace_btn.clicked.connect(self.search_and_replace)
replace_widget.layout().addWidget(self.search_text_input)
replace_widget.layout().addWidget(self.replace_text_input)
replace_widget.layout().addWidget(search_and_replace_btn)
replace_widget.layout().addStretch()
dock.setWidget(replace_widget)
# create status bar widget and atach it to main window:
self.status_bar = qtw.QStatusBar()
self.setStatusBar(self.status_bar)
# show temporary message for 3 second:
self.status_bar.showMessage('Welcome to My Text Editor',3000)
charcount_label = qtw.QLabel("chars: 0")
self.textedit.textChanged.connect(
lambda: charcount_label.setText( "chars: " + str(len(self.textedit.toPlainText())) )
)
self.status_bar.addPermanentWidget(charcount_label)
import sys
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
class MainWindow(qtw.QWidget):
def __init__(self , *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('Icon Demo')
self.setWindowIcon(qtg.QIcon('./icons/calculator.png'))
self.show();
if __name__ == '__main__':
app = qtw.QApplication(sys.argv);
window = MainWindow()
sys.exit(app.exec())
import sys
from PyQt6.QtWidgets import (QApplication, QGridLayout, QPushButton, QStyle, QWidget)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
icons = sorted([attr for attr in dir(QStyle.StandardPixmap) if attr.startswith("SP_")])
layout = QGridLayout()
for n, name in enumerate(icons):
btn = QPushButton(name)
pixmapi = getattr(QStyle.StandardPixmap, name)
icon = self.style().standardIcon(pixmapi)
btn.setIcon(icon)
layout.addWidget(btn, int(n/4), int(n%4))
self.setLayout(layout)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec()
import sys
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtCore as qtc
from PyQt6 import QtGui as qtg
class MainWindow(qtw.QWidget):
def __init__(self , *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('Icons Demo')
### use standard icon:
standardIcon = self.style().standardIcon(getattr(qtw.QStyle.StandardPixmap,'SP_DialogYesButton'))
self.btnStandardIcon = qtw.QPushButton('btnStandardIcon')
self.btnStandardIcon.setIcon(standardIcon)
### use icon from theme:
# https://specifications.freedesktop.org/icon-naming-spec/latest/ar01s04.html
themeIcon = qtg.QIcon.fromTheme("accessories-calculator")
self.btnThemeIcon = qtw.QPushButton('btnThemeIcon')
self.btnThemeIcon.setIcon(themeIcon)
### use custom icon:
customIcon = qtg.QIcon('./icons/submit_icon.png')
self.btnCustomIcon = qtw.QPushButton('btnCustomIcon')
self.btnCustomIcon.setIcon(customIcon)
mainLayout = qtw.QVBoxLayout(self)
mainLayout.addWidget(self.btnCustomIcon)
mainLayout.addWidget(self.btnStandardIcon)
mainLayout.addWidget(self.btnThemeIcon)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv);
window = MainWindow()
window.show()
sys.exit(app.exec())
css file and used as needed
QWidget {
border: 20px solid black;
border-radius: 10px;
background-color: rgb(255, 255, 255);
}
with open("qWidget.css", 'r') as fh:
sheet = fh.read()
widgetInstance.setStyleSheet(sheet)
self.btnSubmit = qtw.QPushButton('Submit')
self.btnSubmit.setObjectName('btnSubmit')
self.btnCancel = qtw.QPushButton('Cancel')
/* apply red background-color to all QPushButton */
QPushButton{
background-color:red;
}
/* apply green background-color only to QPushButton with object name 'btnSubmit' */
QPushButton#btnSubmit{
background-color:green;
}
/* apply red background-color to all QPushButton */
QPushButton{
background-color:red;
}
/* apply blue background-color to buttons which are hovered */
QPushButton:hover{
background-color:blue;
}
setObjectName() method, as we saw before.setStyleSheet()
self.btnCancel = qtw.QPushButton('Cancel')
self.btnCancel.setStyleSheet("""
background-color:gray;
padding:10px;
""")
The whole code for the 'Simple Text Editor App', used in these slides: