Debian Codegraph @

docs/source/notebooks/readme.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Qt-Material\n",
8 "\n",
9 "This is another stylesheet for **PySide6**, **PySide2**, **PyQt5** and **PyQt6**, which looks like Material Design (close enough).\n",
10 "\n",
11 "![GitHub top language](https://img.shields.io/github/languages/top/un-gcpds/qt-material)\n",
12 "![PyPI - License](https://img.shields.io/pypi/l/qt-material)\n",
13 "![PyPI](https://img.shields.io/pypi/v/qt-material)\n",
14 "![PyPI - Status](https://img.shields.io/pypi/status/qt-material)\n",
15 "![PyPI - Python Version](https://img.shields.io/pypi/pyversions/qt-material)\n",
16 "![GitHub last commit](https://img.shields.io/github/last-commit/un-gcpds/qt-material)\n",
17 "![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/UN-GCPDS/qt-material)\n",
18 "[![Documentation Status](https://readthedocs.org/projects/qt-material/badge/?version=latest)](https://qt-material.readthedocs.io/en/latest/?badge=latest)"
19 ]
20 },
21 {
22 "cell_type": "markdown",
23 "metadata": {},
24 "source": [
25 "There is some custom dark themes:\n",
26 "![dark](_images/dark.gif)\n",
27 "And light:\n",
28 "![light](_images/light.gif)"
29 ]
30 },
31 {
32 "cell_type": "markdown",
33 "metadata": {},
34 "source": [
35 "## Navigation\n",
36 "\n",
37 " * [Install](#install)\n",
38 " * [Usage](#usage)\n",
39 " * [Themes](#themes)\n",
40 " * [Custom colors](#custom-colors)\n",
41 " * [Usage](#usage)\n",
42 " * [Light themes](#light-themes)\n",
43 " * [Environ variables](#environ-variables)\n",
44 " * [Alternative QPushButtons and custom fonts](#alternative-qpushbuttons-and-custom-fonts)\n",
45 " * [Custom stylesheets](#custom-stylesheets)\n",
46 " * [Run examples](#run-examples)\n",
47 " * [New themes](#new-themes)\n",
48 " * [Change theme in runtime](#change-theme-in-runtime)\n",
49 " * [Export theme](#export-theme)\n",
50 " * [Density scale](#density-scale)\n",
51 " * [Troubleshoots](#troubleshoots)"
52 ]
53 },
54 {
55 "cell_type": "markdown",
56 "metadata": {},
57 "source": [
58 "## Install"
59 ]
60 },
61 {
62 "cell_type": "code",
63 "execution_count": null,
64 "metadata": {},
65 "outputs": [],
66 "source": [
67 "pip install qt-material"
68 ]
69 },
70 {
71 "cell_type": "markdown",
72 "metadata": {},
73 "source": [
74 "## Usage"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": null,
80 "metadata": {},
81 "outputs": [],
82 "source": [
83 "import sys\n",
84 "from PySide6 import QtWidgets\n",
85 "# from PySide2 import QtWidgets\n",
86 "# from PyQt5 import QtWidgets\n",
87 "from qt_material import apply_stylesheet\n",
88 "\n",
89 "# create the application and the main window\n",
90 "app = QtWidgets.QApplication(sys.argv)\n",
91 "window = QtWidgets.QMainWindow()\n",
92 "\n",
93 "# setup stylesheet\n",
94 "apply_stylesheet(app, theme='dark_teal.xml')\n",
95 "\n",
96 "# run\n",
97 "window.show()\n",
98 "app.exec_()"
99 ]
100 },
101 {
102 "cell_type": "markdown",
103 "metadata": {},
104 "source": [
105 "## Themes"
106 ]
107 },
108 {
109 "cell_type": "code",
110 "execution_count": 1,
111 "metadata": {},
112 "outputs": [
113 {
114 "name": "stderr",
115 "output_type": "stream",
116 "text": [
117 "WARNING:root:qt_material must be imported after PySide or PyQt!\n"
118 ]
119 },
120 {
121 "data": {
122 "text/plain": [
123 "['dark_amber.xml',\n",
124 " 'dark_blue.xml',\n",
125 " 'dark_cyan.xml',\n",
126 " 'dark_lightgreen.xml',\n",
127 " 'dark_pink.xml',\n",
128 " 'dark_purple.xml',\n",
129 " 'dark_red.xml',\n",
130 " 'dark_teal.xml',\n",
131 " 'dark_yellow.xml',\n",
132 " 'light_amber.xml',\n",
133 " 'light_blue.xml',\n",
134 " 'light_cyan.xml',\n",
135 " 'light_cyan_500.xml',\n",
136 " 'light_lightgreen.xml',\n",
137 " 'light_pink.xml',\n",
138 " 'light_purple.xml',\n",
139 " 'light_red.xml',\n",
140 " 'light_teal.xml',\n",
141 " 'light_yellow.xml']"
142 ]
143 },
144 "execution_count": 1,
145 "metadata": {},
146 "output_type": "execute_result"
147 }
148 ],
149 "source": [
150 "from qt_material import list_themes\n",
151 "\n",
152 "list_themes()"
153 ]
154 },
155 {
156 "cell_type": "markdown",
157 "metadata": {},
158 "source": [
159 "## Custom colors\n",
160 "\n",
161 "[Color Tool](https://material.io/resources/color/) is the best way to generate new themes, just choose colors and export as `Android XML`, the theme file must look like:"
162 ]
163 },
164 {
165 "cell_type": "code",
166 "execution_count": null,
167 "metadata": {},
168 "outputs": [],
169 "source": [
170 "<!--?xml version=\"1.0\" encoding=\"UTF-8\"?-->\n",
171 "<resources>\n",
172 "<color name=\"primaryColor\">#00e5ff</color>\n",
173 "<color name=\"primaryLightColor\">#6effff</color>\n",
174 "<color name=\"secondaryColor\">#f5f5f5</color>\n",
175 "<color name=\"secondaryLightColor\">#ffffff</color>\n",
176 "<color name=\"secondaryDarkColor\">#e6e6e6</color>\n",
177 "<color name=\"primaryTextColor\">#000000</color>\n",
178 "<color name=\"secondaryTextColor\">#000000</color>\n",
179 "</resources>"
180 ]
181 },
182 {
183 "cell_type": "markdown",
184 "metadata": {},
185 "source": [
186 "Save it as `my_theme.xml` or similar and apply the style sheet from Python."
187 ]
188 },
189 {
190 "cell_type": "code",
191 "execution_count": null,
192 "metadata": {},
193 "outputs": [],
194 "source": [
195 "apply_stylesheet(app, theme='dark_teal.xml')"
196 ]
197 },
198 {
199 "cell_type": "markdown",
200 "metadata": {},
201 "source": [
202 "## Light themes\n",
203 "Light themes will need to add `invert_secondary` argument as `True`."
204 ]
205 },
206 {
207 "cell_type": "code",
208 "execution_count": null,
209 "metadata": {},
210 "outputs": [],
211 "source": [
212 "apply_stylesheet(app, theme='light_red.xml', invert_secondary=True)"
213 ]
214 },
215 {
216 "cell_type": "markdown",
217 "metadata": {},
218 "source": [
219 "## Environ variables\n",
220 "\n",
221 "There is a environ variables related to the current theme used, these variables are for **consult purpose only**.\n",
222 "\n",
223 "\n",
224 "| Environ variable | Description | Example |\n",
225 "|--------------------------------|------------------------------------------|----------------|\n",
226 "| QTMATERIAL_PRIMARYCOLOR | Primary color | #2979ff |\n",
227 "| QTMATERIAL_PRIMARYLIGHTCOLOR | A bright version of the primary color | #75a7ff |\n",
228 "| QTMATERIAL_SECONDARYCOLOR | Secondary color | #f5f5f5 |\n",
229 "| QTMATERIAL_SECONDARYLIGHTCOLOR | A bright version of the secondary color | #ffffff |\n",
230 "| QTMATERIAL_SECONDARYDARKCOLOR | A dark version of the primary color | #e6e6e6 |\n",
231 "| QTMATERIAL_PRIMARYTEXTCOLOR | Color for text over primary background | #000000 |\n",
232 "| QTMATERIAL_SECONDARYTEXTCOLOR | Color for text over secondary background | #000000 |\n",
233 "| QTMATERIAL_THEME | Name of theme used | light_blue.xml |"
234 ]
235 },
236 {
237 "cell_type": "markdown",
238 "metadata": {},
239 "source": [
240 "## Alternative QPushButtons and custom fonts\n",
241 "\n",
242 "There is an `extra` argument for accent colors and custom fonts. "
243 ]
244 },
245 {
246 "cell_type": "code",
247 "execution_count": null,
248 "metadata": {},
249 "outputs": [],
250 "source": [
251 "extra = {\n",
252 "\n",
253 " # Button colors\n",
254 " 'danger': '#dc3545',\n",
255 " 'warning': '#ffc107',\n",
256 " 'success': '#17a2b8',\n",
257 "\n",
258 " # Font\n",
259 " 'font_family': 'Roboto',\n",
260 "}\n",
261 "\n",
262 "apply_stylesheet(app, 'light_cyan.xml', invert_secondary=True, extra=extra)"
263 ]
264 },
265 {
266 "cell_type": "markdown",
267 "metadata": {},
268 "source": [
269 "The accent colors are applied to `QPushButton` with the corresponding `class` property:"
270 ]
271 },
272 {
273 "cell_type": "code",
274 "execution_count": null,
275 "metadata": {},
276 "outputs": [],
277 "source": [
278 "pushButton_danger.setProperty('class', 'danger')\n",
279 "pushButton_warning.setProperty('class', 'warning')\n",
280 "pushButton_success.setProperty('class', 'success')"
281 ]
282 },
283 {
284 "cell_type": "markdown",
285 "metadata": {},
286 "source": [
287 "![extra](_images/extra.png)"
288 ]
289 },
290 {
291 "cell_type": "markdown",
292 "metadata": {},
293 "source": [
294 "## Custom stylesheets\n",
295 "\n",
296 "Custom changes can be performed by overwriting the stylesheets, for example:"
297 ]
298 },
299 {
300 "cell_type": "code",
301 "execution_count": null,
302 "metadata": {},
303 "outputs": [],
304 "source": [
305 "QPushButton {{\n",
306 " color: {QTMATERIAL_SECONDARYCOLOR};\n",
307 " text-transform: none;\n",
308 " background-color: {QTMATERIAL_PRIMARYCOLOR};\n",
309 "}}\n",
310 "\n",
311 ".big_button {{\n",
312 " height: 64px;\n",
313 "}}"
314 ]
315 },
316 {
317 "cell_type": "markdown",
318 "metadata": {},
319 "source": [
320 "Then, the current stylesheet can be extended just with:"
321 ]
322 },
323 {
324 "cell_type": "code",
325 "execution_count": null,
326 "metadata": {},
327 "outputs": [],
328 "source": [
329 "apply_stylesheet(app, theme='light_blue.xml', css_file='custom.css')"
330 ]
331 },
332 {
333 "cell_type": "markdown",
334 "metadata": {},
335 "source": [
336 "The stylesheet can also be changed on runtime by:"
337 ]
338 },
339 {
340 "cell_type": "code",
341 "execution_count": null,
342 "metadata": {},
343 "outputs": [],
344 "source": [
345 "stylesheet = app.styleSheet()\n",
346 "with open('custom.css') as file:\n",
347 " app.setStyleSheet(stylesheet + file.read().format(**os.environ))"
348 ]
349 },
350 {
351 "cell_type": "markdown",
352 "metadata": {},
353 "source": [
354 "And the class style can be applied with the `setProperty` method:"
355 ]
356 },
357 {
358 "cell_type": "code",
359 "execution_count": null,
360 "metadata": {},
361 "outputs": [],
362 "source": [
363 "self.main.pushButton.setProperty('class', 'big_button')"
364 ]
365 },
366 {
367 "cell_type": "markdown",
368 "metadata": {},
369 "source": [
370 "![extra](_images/custom.png)"
371 ]
372 },
373 {
374 "cell_type": "markdown",
375 "metadata": {},
376 "source": [
377 "## Run examples\n",
378 "A window with almost all widgets (see the previous screenshots) are available to test all themes and **create new ones**."
379 ]
380 },
381 {
382 "cell_type": "code",
383 "execution_count": null,
384 "metadata": {},
385 "outputs": [],
386 "source": [
387 "git clone https://github.com/UN-GCPDS/qt-material.git\n",
388 "cd qt-material\n",
389 "python setup.py install\n",
390 "cd examples/full_features\n",
391 "python main.py --pyside6"
392 ]
393 },
394 {
395 "cell_type": "markdown",
396 "metadata": {},
397 "source": [
398 "![theme](_images/theme.gif)"
399 ]
400 },
401 {
402 "cell_type": "markdown",
403 "metadata": {},
404 "source": [
405 "## New themes\n",
406 "\n",
407 "Do you have a custom theme? it looks good? create a [pull request](https://github.com/UN-GCPDS/qt-material/pulls) in [themes folder](https://github.com/UN-GCPDS/qt-material/tree/master/qt_material/themes>) and share it with all users.\n"
408 ]
409 },
410 {
411 "cell_type": "markdown",
412 "metadata": {},
413 "source": [
414 "## Change theme in runtime\n",
415 "\n",
416 "There is a `qt_material.QtStyleTools` class that must be inherited along to `QMainWindow` for change themes in runtime using the `apply_stylesheet()` method."
417 ]
418 },
419 {
420 "cell_type": "code",
421 "execution_count": null,
422 "metadata": {},
423 "outputs": [],
424 "source": [
425 "class RuntimeStylesheets(QMainWindow, QtStyleTools):\n",
426 " \n",
427 " def __init__(self):\n",
428 " super().__init__()\n",
429 " self.main = QUiLoader().load('main_window.ui', self)\n",
430 " \n",
431 " self.apply_stylesheet(self.main, 'dark_teal.xml')\n",
432 " # self.apply_stylesheet(self.main, 'light_red.xml')\n",
433 " # self.apply_stylesheet(self.main, 'light_blue.xml')"
434 ]
435 },
436 {
437 "cell_type": "markdown",
438 "metadata": {},
439 "source": [
440 "![run](_images/runtime.gif)"
441 ]
442 },
443 {
444 "cell_type": "markdown",
445 "metadata": {},
446 "source": [
447 "### Integrate stylesheets in a menu\n",
448 "\n",
449 "A custom _stylesheets menu_ can be added to a project for switching across all default available themes."
450 ]
451 },
452 {
453 "cell_type": "code",
454 "execution_count": null,
455 "metadata": {},
456 "outputs": [],
457 "source": [
458 "class RuntimeStylesheets(QMainWindow, QtStyleTools):\n",
459 " \n",
460 " def __init__(self):\n",
461 " super().__init__()\n",
462 " self.main = QUiLoader().load('main_window.ui', self)\n",
463 " \n",
464 " self.add_menu_theme(self.main, self.main.menuStyles)"
465 ]
466 },
467 {
468 "cell_type": "markdown",
469 "metadata": {},
470 "source": [
471 "![menu](_images/runtime_menu.gif)"
472 ]
473 },
474 {
475 "cell_type": "markdown",
476 "metadata": {},
477 "source": [
478 "## Create new themes\n",
479 "\n",
480 "A simple interface is available to modify a theme in runtime, this feature can be used to create a new theme, the theme file is created in the main directory as `my_theme.xml`"
481 ]
482 },
483 {
484 "cell_type": "code",
485 "execution_count": null,
486 "metadata": {},
487 "outputs": [],
488 "source": [
489 "class RuntimeStylesheets(QMainWindow, QtStyleTools):\n",
490 " \n",
491 " def __init__(self):\n",
492 " super().__init__()\n",
493 " self.main = QUiLoader().load('main_window.ui', self)\n",
494 " \n",
495 " self.show_dock_theme(self.main)"
496 ]
497 },
498 {
499 "cell_type": "markdown",
500 "metadata": {},
501 "source": [
502 "![dock](_images/runtime_dock.gif)"
503 ]
504 },
505 {
506 "cell_type": "markdown",
507 "metadata": {},
508 "source": [
509 "A full set of examples are available in the [exmaples directory](https://github.com/UN-GCPDS/qt-material/blob/master/examples/runtime/)"
510 ]
511 },
512 {
513 "cell_type": "markdown",
514 "metadata": {},
515 "source": [
516 "## Export theme\n",
517 "\n",
518 "This feature able to use ```qt-material``` themes into ```Qt``` implementations using only local files."
519 ]
520 },
521 {
522 "cell_type": "code",
523 "execution_count": null,
524 "metadata": {},
525 "outputs": [],
526 "source": [
527 "from qt_material import export_theme\n",
528 "\n",
529 "extra = {\n",
530 "\n",
531 " # Button colors\n",
532 " 'danger': '#dc3545',\n",
533 " 'warning': '#ffc107',\n",
534 " 'success': '#17a2b8',\n",
535 "\n",
536 " # Font\n",
537 " 'font_family': 'monoespace',\n",
538 " 'font_size': '13px',\n",
539 " 'line_height': '13px',\n",
540 "\n",
541 " # Density Scale\n",
542 " 'density_scale': '0',\n",
543 "\n",
544 " # environ\n",
545 " 'pyside6': True,\n",
546 " 'linux': True,\n",
547 "\n",
548 "}\n",
549 "\n",
550 "export_theme(theme='dark_teal.xml', \n",
551 " qss='dark_teal.qss', \n",
552 " rcc='resources.rcc',\n",
553 " output='theme', \n",
554 " prefix='icon:/', \n",
555 " invert_secondary=False, \n",
556 " extra=extra,\n",
557 " )"
558 ]
559 },
560 {
561 "cell_type": "markdown",
562 "metadata": {},
563 "source": [
564 "This script will generate both ```dark_teal.qss``` and ```resources.rcc``` and a folder with all theme icons called ```theme```.\n",
565 "\n",
566 "The files generated can be integrated into a ```PySide6``` application just with:"
567 ]
568 },
569 {
570 "cell_type": "code",
571 "execution_count": null,
572 "metadata": {},
573 "outputs": [],
574 "source": [
575 "import sys\n",
576 "\n",
577 "from PySide6 import QtWidgets\n",
578 "from PySide6.QtCore import QDir\n",
579 "from __feature__ import snake_case, true_property\n",
580 "\n",
581 "# Create application\n",
582 "app = QtWidgets.QApplication(sys.argv)\n",
583 "\n",
584 "# Load styles\n",
585 "with open('dark_teal.qss', 'r') as file:\n",
586 " app.style_sheet = file.read()\n",
587 "\n",
588 "# Load icons\n",
589 "QDir.add_search_path('icon', 'theme')\n",
590 "\n",
591 "# App\n",
592 "window = QtWidgets.QMainWindow()\n",
593 "checkbox = QtWidgets.QCheckBox(window)\n",
594 "checkbox.text = 'CheckBox'\n",
595 "window.show()\n",
596 "app.exec()"
597 ]
598 },
599 {
600 "cell_type": "markdown",
601 "metadata": {},
602 "source": [
603 "This files can also be used into non ```Python``` environs like ```C++```."
604 ]
605 },
606 {
607 "cell_type": "markdown",
608 "metadata": {},
609 "source": [
610 "## Density scale\n",
611 "\n",
612 "The ``extra`` arguments also include an option to set the **density scale**, by default is ```0```."
613 ]
614 },
615 {
616 "cell_type": "code",
617 "execution_count": null,
618 "metadata": {},
619 "outputs": [],
620 "source": [
621 "extra = {\n",
622 " \n",
623 " # Density Scale\n",
624 " 'density_scale': '-2',\n",
625 "}\n",
626 "\n",
627 "apply_stylesheet(app, 'default', invert_secondary=False, extra=extra)"
628 ]
629 },
630 {
631 "cell_type": "markdown",
632 "metadata": {},
633 "source": [
634 "![dock](_images/density/density.gif)"
635 ]
636 },
637 {
638 "cell_type": "markdown",
639 "metadata": {},
640 "source": [
641 "## Troubleshoots"
642 ]
643 },
644 {
645 "cell_type": "markdown",
646 "metadata": {},
647 "source": [
648 "### QMenu\n",
649 "\n",
650 "`QMenu` has multiple rendering for each Qt backend, and for each operating system. Even can be related with the style, like [fusion](https://doc.qt.io/qt-5/qtquickcontrols2-fusion.html). Then, the `extra` argument also supports`QMenu` parameters to configure this widgest for specific combinations. This options are not affected by **density scale**."
651 ]
652 },
653 {
654 "cell_type": "code",
655 "execution_count": null,
656 "metadata": {},
657 "outputs": [],
658 "source": [
659 "extra['QMenu'] = {\n",
660 " 'height': 50,\n",
661 " 'padding': '50px 50px 50px 50px', # top, right, bottom, left\n",
662 "}"
663 ]
664 }
665 ],
666 "metadata": {
667 "kernelspec": {
668 "display_name": "Python 3 (ipykernel)",
669 "language": "python",
670 "name": "python3"
671 },
672 "language_info": {
673 "codemirror_mode": {
674 "name": "ipython",
675 "version": 3
676 },
677 "file_extension": ".py",
678 "mimetype": "text/x-python",
679 "name": "python",
680 "nbconvert_exporter": "python",
681 "pygments_lexer": "ipython3",
682 "version": "3.10.9"
683 }
684 },
685 "nbformat": 4,
686 "nbformat_minor": 4
687 }