设置图片背景的主题 :param cls: :param image: 背景图片 :param widget: 指定控件
(cls, image=None, widget=None, replaces={})
| 347 | |
| 348 | @classmethod |
| 349 | def loadPictureTheme(cls, image=None, widget=None, replaces={}): |
| 350 | """设置图片背景的主题 |
| 351 | :param cls: |
| 352 | :param image: 背景图片 |
| 353 | :param widget: 指定控件 |
| 354 | """ |
| 355 | # 加载主题取样式 |
| 356 | path = cls.stylePath('Default') |
| 357 | AppLog.info('stylePath: {}'.format(path)) |
| 358 | try: |
| 359 | styleSheet = open(path, 'rb').read().decode('utf-8', |
| 360 | errors='ignore') |
| 361 | # 需要替换部分样式 |
| 362 | if image and os.path.isfile(image): |
| 363 | # 获取图片主色调 |
| 364 | color_thief = ColorThief(image) |
| 365 | color = color_thief.get_color() |
| 366 | AppLog.info('dominant color: {}'.format(str(color))) |
| 367 | |
| 368 | # 替换name |
| 369 | templates = StylePictureTemplate |
| 370 | for name, value in replaces.items(): |
| 371 | templates = templates.replace(name, value) |
| 372 | |
| 373 | styleSheet += templates.format( |
| 374 | os.path.abspath(image).replace( |
| 375 | '\\', '/')) + StyleColorTemplate.format(*color) |
| 376 | widget = widget or QApplication.instance() |
| 377 | widget.setStyleSheet(styleSheet) |
| 378 | except Exception as e: |
| 379 | AppLog.exception(e) |
| 380 | |
| 381 | @classmethod |
| 382 | def loadCursor(cls, widget, name='default.png'): |