mirror of
https://github.com/UN-GCPDS/qt-material.git
synced 2025-01-13 16:42:56 +08:00
Update example
This commit is contained in:
parent
ea5edd216b
commit
2b1bc2bc5e
@ -6,7 +6,7 @@ themes = list_themes()
|
||||
themes = [t.replace('.xml', '') for t in themes]
|
||||
|
||||
for theme in themes:
|
||||
os.system(f'python main.py --pyside {theme}')
|
||||
os.system(f'python main.py --pyside6 {theme}')
|
||||
|
||||
os.chdir('screenshots')
|
||||
|
||||
@ -17,6 +17,7 @@ commands = (
|
||||
'rm ../../docs/source/images/dark.gif',
|
||||
'cp light.gif ../../docs/source/images/light.gif',
|
||||
'cp dark.gif ../../docs/source/images/dark.gif',
|
||||
'cp theme.png ../../docs/source/images/theme.png',
|
||||
)
|
||||
|
||||
for command in commands:
|
||||
|
145
test/main.py
145
test/main.py
@ -1,28 +1,27 @@
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
|
||||
from multiprocessing import freeze_support
|
||||
import psutil
|
||||
import signal
|
||||
|
||||
if '--pyside2' in sys.argv:
|
||||
from PySide2.QtWidgets import QApplication
|
||||
from PySide2 import QtWidgets
|
||||
from PySide2.QtCore import QTimer
|
||||
from PySide2.QtWidgets import QApplication, QMainWindow, QColorDialog
|
||||
from PySide2.QtCore import QTimer, Qt, QCoreApplication
|
||||
from PySide2.QtGui import QColor
|
||||
from PySide2.QtUiTools import QUiLoader
|
||||
from PySide2.QtCore import Qt, QCoreApplication
|
||||
|
||||
elif '--pyside6' in sys.argv:
|
||||
from PySide6.QtWidgets import QApplication
|
||||
from PySide6 import QtWidgets
|
||||
from PySide6.QtCore import QTimer
|
||||
from PySide6.QtWidgets import QApplication, QMainWindow, QColorDialog
|
||||
from PySide6.QtCore import QTimer, Qt, QCoreApplication
|
||||
from PySide6.QtGui import QColor
|
||||
from PySide6.QtUiTools import QUiLoader
|
||||
from PySide6.QtCore import Qt, QCoreApplication
|
||||
|
||||
elif '--pyqt5' in sys.argv:
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
from PyQt5 import QtWidgets, uic
|
||||
from PyQt5.QtCore import QTimer
|
||||
from PyQt5.QtCore import Qt, QCoreApplication
|
||||
from PyQt5 import QtWebEngineWidgets
|
||||
from PyQt5.QtWidgets import QApplication, QMainWindow, QColorDialog
|
||||
from PyQt5.QtCore import QTimer, Qt, QCoreApplication
|
||||
from PyQt5.QtGui import QColor
|
||||
from PyQt5 import uic, QtWebEngineWidgets
|
||||
|
||||
|
||||
from qt_material import apply_stylesheet, QtStyleSwitcher
|
||||
@ -37,25 +36,41 @@ elif 'PySide6' in sys.modules:
|
||||
elif 'Qt' in sys.modules:
|
||||
from qt_material.resources import logos_pyqt5_rc
|
||||
|
||||
freeze_support()
|
||||
QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
|
||||
|
||||
app = QApplication([])
|
||||
app.processEvents()
|
||||
app.setQuitOnLastWindowClosed(False)
|
||||
# app.lastWindowClosed.connect(kill_childs)
|
||||
# app.lastWindowClosed.connect(lambda: app.quit())
|
||||
|
||||
app.setStyle('Fusion') # For better looking
|
||||
|
||||
extra = {'danger': '#dc3545',
|
||||
'warning': '#ffc107',
|
||||
'success': '#17a2b8',
|
||||
}
|
||||
|
||||
|
||||
########################################################################
|
||||
class RuntimeStylesheets(QtWidgets.QMainWindow, QtStyleSwitcher):
|
||||
class RuntimeStylesheets(QMainWindow, QtStyleSwitcher):
|
||||
# ----------------------------------------------------------------------
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
super().__init__()
|
||||
|
||||
# Extra stylesheets
|
||||
self.extra = {'danger': '#dc3545',
|
||||
'warning': '#ffc107',
|
||||
'success': '#17a2b8',
|
||||
}
|
||||
|
||||
self.colors = ['primaryColor',
|
||||
'primaryLightColor',
|
||||
'secondaryColor',
|
||||
'secondaryLightColor',
|
||||
'secondaryDarkColor',
|
||||
'primaryTextColor',
|
||||
'secondaryTextColor']
|
||||
|
||||
self.custom_colors = {v: os.environ[f'PYSIDEMATERIAL_{v.upper()}'] for v in self.colors}
|
||||
|
||||
if '--pyside2' in sys.argv:
|
||||
self.main = QUiLoader().load('main_window.ui', self)
|
||||
@ -74,7 +89,33 @@ class RuntimeStylesheets(QtWidgets.QMainWindow, QtStyleSwitcher):
|
||||
sys.exit()
|
||||
|
||||
self.custom_styles()
|
||||
self.set_style_switcher(self.main, self.main.menuStyles, self.extra)
|
||||
self.update_buttons()
|
||||
self.set_style_switcher(self.main, self.main.menuStyles, extra, self.update_buttons)
|
||||
|
||||
self.main.checkBox_ligh_theme.clicked.connect(self.update_theme)
|
||||
|
||||
for color in self.colors:
|
||||
button = getattr(self.main, f'pushButton_{color}')
|
||||
button.clicked.connect(self.set_color(color))
|
||||
|
||||
self.main.dockWidget_theme.setFloating(True)
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
def set_color(self, button_):
|
||||
""""""
|
||||
def iner():
|
||||
initial = self.get_color(self.custom_colors[button_])
|
||||
color_dialog = QColorDialog(parent=self)
|
||||
color_dialog.setCurrentColor(initial)
|
||||
done = color_dialog.exec_()
|
||||
color_ = color_dialog.currentColor()
|
||||
|
||||
if done and color_.isValid():
|
||||
color = '#' + ''.join([hex(v)[2:].ljust(2, '0') for v in color_.toTuple()[:3]])
|
||||
self.custom_colors[button_] = color
|
||||
self.update_theme()
|
||||
|
||||
return iner
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
def custom_styles(self):
|
||||
@ -84,6 +125,64 @@ class RuntimeStylesheets(QtWidgets.QMainWindow, QtStyleSwitcher):
|
||||
tool_button.setMaximumWidth(150)
|
||||
tool_button.setMinimumWidth(150)
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
def update_theme(self, event=None):
|
||||
""""""
|
||||
with open('my_theme.xml', 'w') as file:
|
||||
file.write("""
|
||||
<resources>
|
||||
<color name="primaryColor">{primaryColor}</color>
|
||||
<color name="primaryLightColor">{primaryLightColor}</color>
|
||||
<color name="secondaryColor">{secondaryColor}</color>
|
||||
<color name="secondaryLightColor">{secondaryLightColor}</color>
|
||||
<color name="secondaryDarkColor">{secondaryDarkColor}</color>
|
||||
<color name="primaryTextColor">{primaryTextColor}</color>
|
||||
<color name="secondaryTextColor">{secondaryTextColor}</color>
|
||||
</resources>
|
||||
""".format(**self.custom_colors))
|
||||
|
||||
light = self.main.checkBox_ligh_theme.isChecked()
|
||||
self.apply_stylesheet(self.main, 'my_theme.xml', invert_secondary=light, extra=extra, callable_=self.update_buttons)
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
def update_buttons(self):
|
||||
""""""
|
||||
theme = {color_: os.environ[f'PYSIDEMATERIAL_{color_.upper()}'] for color_ in self.colors}
|
||||
|
||||
if 'light' in os.environ['PYSIDEMATERIAL_THEME']:
|
||||
self.main.checkBox_ligh_theme.setChecked(True)
|
||||
elif 'dark' in os.environ['PYSIDEMATERIAL_THEME']:
|
||||
self.main.checkBox_ligh_theme.setChecked(False)
|
||||
|
||||
if self.main.checkBox_ligh_theme.isChecked():
|
||||
theme['secondaryColor'], theme['secondaryLightColor'], theme['secondaryDarkColor'] = theme[
|
||||
'secondaryColor'], theme['secondaryDarkColor'], theme['secondaryLightColor']
|
||||
# theme['primaryTextColor'] = theme['secondaryTextColor']
|
||||
|
||||
for color_ in self.colors:
|
||||
button = getattr(self.main, f'pushButton_{color_}')
|
||||
|
||||
color = theme[color_]
|
||||
|
||||
if self.get_color(color).getHsv()[2] < 128:
|
||||
text_color = '#ffffff'
|
||||
else:
|
||||
text_color = '#000000'
|
||||
|
||||
button.setStyleSheet(f"""
|
||||
*{{
|
||||
background-color: {color};
|
||||
color: {text_color};
|
||||
border: none;
|
||||
}}""")
|
||||
|
||||
self.custom_colors[color_] = color
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
def get_color(self, color):
|
||||
""""""
|
||||
return QColor(*[int(color[s:s + 2], 16) for s in range(1, 6, 2)])
|
||||
|
||||
|
||||
T0 = 1000
|
||||
|
||||
@ -101,11 +200,13 @@ if __name__ == "__main__":
|
||||
QTimer.singleShot(T0 * 2, app.closeAllWindows)
|
||||
except:
|
||||
theme = 'default'
|
||||
theme = 'default_light'
|
||||
# theme = 'default_light'
|
||||
|
||||
# Set theme on in itialization
|
||||
apply_stylesheet(app, theme + '.xml',
|
||||
light_secondary=('light' in theme and 'dark' not in theme))
|
||||
invert_secondary=('light' in theme and 'dark' not in theme),
|
||||
extra=extra)
|
||||
# QIcon.setThemeName("breeze-dark")
|
||||
|
||||
frame = RuntimeStylesheets()
|
||||
frame.main.show()
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user