1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-17 17:42:20 +03:00

Prevent plugin widget code issues from breaking theme changes

This adds a try/except around a bunch of .configure() calls on widgets.
Yes, sure, this could then leave the widget in question in a broken state,
but at least our entire UI won't fall flat on its face.
This commit is contained in:
Athanasius 2022-02-19 11:15:06 +00:00
parent 6a9645d43a
commit 10977ccb07
No known key found for this signature in database
GPG Key ID: AE3E527847057C7D

105
theme.py
View File

@ -14,6 +14,9 @@ from tkinter import ttk
from config import config from config import config
from ttkHyperlinkLabel import HyperlinkLabel from ttkHyperlinkLabel import HyperlinkLabel
from EDMCLogging import get_main_logger
logger = get_main_logger()
if __debug__: if __debug__:
from traceback import print_exc from traceback import print_exc
@ -266,50 +269,64 @@ class _Theme(object):
widget.winfo_class(), widget, 'text' in widget.keys() and widget['text']) widget.winfo_class(), widget, 'text' in widget.keys() and widget['text'])
attribs = self.widgets.get(widget, []) attribs = self.widgets.get(widget, [])
if isinstance(widget, tk.BitmapImage): try:
# not a widget if isinstance(widget, tk.BitmapImage):
if 'fg' not in attribs: # not a widget
widget.configure(foreground=self.current['foreground']), if 'fg' not in attribs:
if 'bg' not in attribs: widget.configure(foreground=self.current['foreground']),
widget.configure(background=self.current['background'])
elif 'cursor' in widget.keys() and str(widget['cursor']) not in ['', 'arrow']: if 'bg' not in attribs:
# Hack - highlight widgets like HyperlinkLabel with a non-default cursor widget.configure(background=self.current['background'])
if 'fg' not in attribs:
widget.configure(foreground=self.current['highlight']), elif 'cursor' in widget.keys() and str(widget['cursor']) not in ['', 'arrow']:
if 'insertbackground' in widget.keys(): # tk.Entry # Hack - highlight widgets like HyperlinkLabel with a non-default cursor
widget.configure(insertbackground=self.current['foreground']), if 'fg' not in attribs:
if 'bg' not in attribs: widget.configure(foreground=self.current['highlight']),
widget.configure(background=self.current['background']) if 'insertbackground' in widget.keys(): # tk.Entry
if 'highlightbackground' in widget.keys(): # tk.Entry widget.configure(insertbackground=self.current['foreground']),
widget.configure(highlightbackground=self.current['background'])
if 'font' not in attribs: if 'bg' not in attribs:
widget.configure(font=self.current['font']) widget.configure(background=self.current['background'])
elif 'activeforeground' in widget.keys(): if 'highlightbackground' in widget.keys(): # tk.Entry
# e.g. tk.Button, tk.Label, tk.Menu widget.configure(highlightbackground=self.current['background'])
if 'fg' not in attribs:
widget.configure(foreground=self.current['foreground'], if 'font' not in attribs:
activeforeground=self.current['activeforeground'], widget.configure(font=self.current['font'])
disabledforeground=self.current['disabledforeground'])
if 'bg' not in attribs: elif 'activeforeground' in widget.keys():
widget.configure(background=self.current['background'], # e.g. tk.Button, tk.Label, tk.Menu
activebackground=self.current['activebackground']) if 'fg' not in attribs:
if sys.platform == 'darwin' and isinstance(widget, tk.Button): widget.configure(foreground=self.current['foreground'],
widget.configure(highlightbackground=self.current['background']) activeforeground=self.current['activeforeground'],
if 'font' not in attribs: disabledforeground=self.current['disabledforeground'])
widget.configure(font=self.current['font'])
elif 'foreground' in widget.keys(): if 'bg' not in attribs:
# e.g. ttk.Label widget.configure(background=self.current['background'],
if 'fg' not in attribs: activebackground=self.current['activebackground'])
widget.configure(foreground=self.current['foreground']), if sys.platform == 'darwin' and isinstance(widget, tk.Button):
if 'bg' not in attribs: widget.configure(highlightbackground=self.current['background'])
widget.configure(background=self.current['background'])
if 'font' not in attribs: if 'font' not in attribs:
widget.configure(font=self.current['font']) widget.configure(font=self.current['font'])
elif 'background' in widget.keys() or isinstance(widget, tk.Canvas): elif 'foreground' in widget.keys():
# e.g. Frame, Canvas # e.g. ttk.Label
if 'bg' not in attribs: if 'fg' not in attribs:
widget.configure(background=self.current['background'], widget.configure(foreground=self.current['foreground']),
highlightbackground=self.current['disabledforeground'])
if 'bg' not in attribs:
widget.configure(background=self.current['background'])
if 'font' not in attribs:
widget.configure(font=self.current['font'])
elif 'background' in widget.keys() or isinstance(widget, tk.Canvas):
# e.g. Frame, Canvas
if 'bg' not in attribs:
widget.configure(background=self.current['background'],
highlightbackground=self.current['disabledforeground'])
except Exception:
logger.exception('Plugin widget issue ?')
# Apply configured theme # Apply configured theme