1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-15 00:30:33 +03:00

[2186] Simplify myNB Files

This commit is contained in:
David Sangrey 2024-03-27 21:22:35 -04:00
parent f7b39f8daf
commit c1b8533cb4
No known key found for this signature in database
GPG Key ID: 3AEADBB0186884BC
6 changed files with 62 additions and 79 deletions

View File

@ -7,6 +7,8 @@ from __future__ import annotations
import logging import logging
import tkinter as tk import tkinter as tk
from tkinter import ttk
import myNotebook as nb # noqa: N813 import myNotebook as nb # noqa: N813
from config import appname, config from config import appname, config
@ -63,7 +65,7 @@ class ClickCounter:
# setup our config in a "Click Count: number" # setup our config in a "Click Count: number"
nb.Label(frame, text='Click Count').grid(row=current_row) nb.Label(frame, text='Click Count').grid(row=current_row)
nb.Entry(frame, textvariable=self.click_count).grid(row=current_row, column=1) ttk.Entry(frame, textvariable=self.click_count).grid(row=current_row, column=1)
current_row += 1 # Always increment our row counter, makes for far easier tkinter design. current_row += 1 # Always increment our row counter, makes for far easier tkinter design.
return frame return frame

View File

@ -1,12 +1,9 @@
""" """
Custom `ttk.Notebook` to fix various display issues. Custom `ttk.Notebook` to fix various display issues.
Hacks to fix various display issues with notebooks and their child widgets on Hacks to fix various display issues with notebooks and their child widgets on Windows.
OSX and Windows.
- Windows: page background should be White, not SystemButtonFace - Windows: page background should be White, not SystemButtonFace
- OSX: page background should be a darker gray than systemWindowBody
selected tab foreground should be White when the window is active
Entire file may be imported by plugins. Entire file may be imported by plugins.
""" """
@ -26,24 +23,17 @@ class Notebook(ttk.Notebook):
def __init__(self, master: ttk.Frame | None = None, **kw): def __init__(self, master: ttk.Frame | None = None, **kw):
ttk.Notebook.__init__(self, master, **kw) super().__init__(master, **kw)
style = ttk.Style() style = ttk.Style()
if sys.platform == 'win32':
style.configure('nb.TFrame', background=PAGEBG) style.configure('nb.TFrame', background=PAGEBG)
style.configure('nb.TButton', background=PAGEBG) style.configure('nb.TButton', background=PAGEBG)
style.configure('nb.TCheckbutton', foreground=PAGEFG, background=PAGEBG) style.configure('nb.TCheckbutton', foreground=PAGEFG, background=PAGEBG)
style.configure('nb.TMenubutton', foreground=PAGEFG, background=PAGEBG) style.configure('nb.TMenubutton', foreground=PAGEFG, background=PAGEBG)
style.configure('nb.TRadiobutton', foreground=PAGEFG, background=PAGEBG) style.configure('nb.TRadiobutton', foreground=PAGEFG, background=PAGEBG)
self.grid(padx=10, pady=10, sticky=tk.NSEW) self.grid(padx=10, pady=10, sticky=tk.NSEW)
else:
self.grid(padx=10, pady=10, sticky=tk.NSEW)
# FIXME: The real fix for this 'dynamic type' would be to split this whole class Frame(tk.Frame or ttk.Frame): # type: ignore
# thing into being a module with per-platform files, as we've done with config
# That would also make the code cleaner.
class Frame(sys.platform == 'darwin' and tk.Frame or ttk.Frame): # type: ignore
"""Custom t(t)k.Frame class to fix some display issues.""" """Custom t(t)k.Frame class to fix some display issues."""
def __init__(self, master: ttk.Notebook | None = None, **kw): def __init__(self, master: ttk.Notebook | None = None, **kw):
@ -60,26 +50,25 @@ class Label(tk.Label):
"""Custom tk.Label class to fix some display issues.""" """Custom tk.Label class to fix some display issues."""
def __init__(self, master: ttk.Frame | None = None, **kw): def __init__(self, master: ttk.Frame | None = None, **kw):
# This format chosen over `sys.platform in (...)` as mypy and friends dont understand that kw['foreground'] = kw.pop('foreground', PAGEFG if sys.platform == 'win32'
if sys.platform == 'win32': else ttk.Style().lookup('TLabel', 'foreground'))
kw['foreground'] = kw.pop('foreground', PAGEFG) kw['background'] = kw.pop('background', PAGEBG if sys.platform == 'win32'
kw['background'] = kw.pop('background', PAGEBG) else ttk.Style().lookup('TLabel', 'background'))
else: super().__init__(master, **kw)
kw['foreground'] = kw.pop('foreground', ttk.Style().lookup('TLabel', 'foreground'))
kw['background'] = kw.pop('background', ttk.Style().lookup('TLabel', 'background'))
tk.Label.__init__(self, master, **kw) # Just use tk.Label on all platforms
class Entry(sys.platform == 'darwin' and tk.Entry or ttk.Entry): # type: ignore class Entry(ttk.Entry): # type: ignore
"""Custom t(t)k.Entry class to fix some display issues.""" """Custom t(t)k.Entry class to fix some display issues."""
# DEPRECATED: Migrate to ttk.Entry. Will remove in 5.12 or later.
def __init__(self, master: ttk.Frame | None = None, **kw): def __init__(self, master: ttk.Frame | None = None, **kw):
ttk.Entry.__init__(self, master, **kw) super().__init__(master, **kw)
class Button(sys.platform == 'darwin' and tk.Button or ttk.Button): # type: ignore class Button(tk.Button or ttk.Button): # type: ignore
"""Custom t(t)k.Button class to fix some display issues.""" """Custom t(t)k.Button class to fix some display issues."""
# DEPRECATED: Migrate to ttk.Button. Will remove in 5.12 or later.
def __init__(self, master: ttk.Frame | None = None, **kw): def __init__(self, master: ttk.Frame | None = None, **kw):
if sys.platform == 'win32': if sys.platform == 'win32':
ttk.Button.__init__(self, master, style='nb.TButton', **kw) ttk.Button.__init__(self, master, style='nb.TButton', **kw)
@ -87,47 +76,39 @@ class Button(sys.platform == 'darwin' and tk.Button or ttk.Button): # type: ign
ttk.Button.__init__(self, master, **kw) ttk.Button.__init__(self, master, **kw)
class ColoredButton(sys.platform == 'darwin' and tk.Label or tk.Button): # type: ignore class ColoredButton(tk.Label or tk.Button): # type: ignore
"""Custom t(t)k.ColoredButton class to fix some display issues.""" """Custom t(t)k.ColoredButton class to fix some display issues."""
# DEPRECATED: Migrate to tk.Button. Will remove in 5.12 or later.
def __init__(self, master: ttk.Frame | None = None, **kw): def __init__(self, master: ttk.Frame | None = None, **kw):
tk.Button.__init__(self, master, **kw) tk.Button.__init__(self, master, **kw)
class Checkbutton(sys.platform == 'darwin' and tk.Checkbutton or ttk.Checkbutton): # type: ignore class Checkbutton(ttk.Checkbutton):
"""Custom t(t)k.Checkbutton class to fix some display issues.""" """Custom t(t)k.Checkbutton class to fix some display issues."""
def __init__(self, master: ttk.Frame | None = None, **kw): def __init__(self, master=None, **kw):
if sys.platform == 'win32': style = 'nb.TCheckbutton' if sys.platform == 'win32' else None
ttk.Checkbutton.__init__(self, master, style='nb.TCheckbutton', **kw) super().__init__(master, style=style, **kw) # type: ignore
else:
ttk.Checkbutton.__init__(self, master, **kw)
class Radiobutton(sys.platform == 'darwin' and tk.Radiobutton or ttk.Radiobutton): # type: ignore class Radiobutton(ttk.Radiobutton):
"""Custom t(t)k.Radiobutton class to fix some display issues.""" """Custom t(t)k.Radiobutton class to fix some display issues."""
def __init__(self, master: ttk.Frame | None = None, **kw): def __init__(self, master: ttk.Frame | None = None, **kw):
if sys.platform == 'win32': style = 'nb.TRadiobutton' if sys.platform == 'win32' else None
ttk.Radiobutton.__init__(self, master, style='nb.TRadiobutton', **kw) super().__init__(master, style=style, **kw) # type: ignore
else:
ttk.Radiobutton.__init__(self, master, **kw)
class OptionMenu(sys.platform == 'darwin' and tk.OptionMenu or ttk.OptionMenu): # type: ignore class OptionMenu(ttk.OptionMenu):
"""Custom t(t)k.OptionMenu class to fix some display issues.""" """Custom ttk.OptionMenu class to fix some display issues."""
def __init__(self, master, variable, default=None, *values, **kw): def __init__(self, master, variable, default=None, *values, **kw):
if sys.platform == 'win32': style = 'nb.TMenubutton' if sys.platform == 'win32' else ttk.Style().lookup('TMenu', 'background')
# OptionMenu derives from Menubutton at the Python level, so uses Menubutton's style menu_background = PAGEBG if sys.platform == 'win32' else ttk.Style().lookup('TMenu', 'background')
ttk.OptionMenu.__init__(self, master, variable, default, *values, style='nb.TMenubutton', **kw)
self['menu'].configure(background=PAGEBG) super().__init__(master, variable, default, *values, style=style, **kw)
# Workaround for https://bugs.python.org/issue25684 self['menu'].configure(background=menu_background)
for i in range(0, self['menu'].index('end')+1):
self['menu'].entryconfig(i, variable=variable) for i in range(self['menu'].index('end') + 1):
else:
ttk.OptionMenu.__init__(self, master, variable, default, *values, **kw)
self['menu'].configure(background=ttk.Style().lookup('TMenu', 'background'))
# Workaround for https://bugs.python.org/issue25684
for i in range(0, self['menu'].index('end')+1):
self['menu'].entryconfig(i, variable=variable) self['menu'].entryconfig(i, variable=variable)

View File

@ -106,12 +106,12 @@ def plugin_prefs(parent: ttk.Notebook, cmdr: str | None, is_beta: bool) -> tk.Fr
# LANG: Settings>Coriolis: Label for 'NOT alpha/beta game version' URL # LANG: Settings>Coriolis: Label for 'NOT alpha/beta game version' URL
nb.Label(conf_frame, text=_('Normal URL')).grid(sticky=tk.W, row=cur_row, column=0, padx=PADX, pady=PADY) nb.Label(conf_frame, text=_('Normal URL')).grid(sticky=tk.W, row=cur_row, column=0, padx=PADX, pady=PADY)
nb.Entry(conf_frame, ttk.Entry(conf_frame,
textvariable=coriolis_config.normal_textvar).grid( textvariable=coriolis_config.normal_textvar).grid(
sticky=tk.EW, row=cur_row, column=1, padx=PADX, pady=BOXY sticky=tk.EW, row=cur_row, column=1, padx=PADX, pady=BOXY
) )
# LANG: Generic 'Reset' button label # LANG: Generic 'Reset' button label
nb.Button(conf_frame, text=_("Reset"), ttk.Button(conf_frame, text=_("Reset"),
command=lambda: coriolis_config.normal_textvar.set(value=DEFAULT_NORMAL_URL)).grid( command=lambda: coriolis_config.normal_textvar.set(value=DEFAULT_NORMAL_URL)).grid(
sticky=tk.W, row=cur_row, column=2, padx=PADX, pady=0 sticky=tk.W, row=cur_row, column=2, padx=PADX, pady=0
) )
@ -119,11 +119,11 @@ def plugin_prefs(parent: ttk.Notebook, cmdr: str | None, is_beta: bool) -> tk.Fr
# LANG: Settings>Coriolis: Label for 'alpha/beta game version' URL # LANG: Settings>Coriolis: Label for 'alpha/beta game version' URL
nb.Label(conf_frame, text=_('Beta URL')).grid(sticky=tk.W, row=cur_row, column=0, padx=PADX, pady=PADY) nb.Label(conf_frame, text=_('Beta URL')).grid(sticky=tk.W, row=cur_row, column=0, padx=PADX, pady=PADY)
nb.Entry(conf_frame, textvariable=coriolis_config.beta_textvar).grid( ttk.Entry(conf_frame, textvariable=coriolis_config.beta_textvar).grid(
sticky=tk.EW, row=cur_row, column=1, padx=PADX, pady=BOXY sticky=tk.EW, row=cur_row, column=1, padx=PADX, pady=BOXY
) )
# LANG: Generic 'Reset' button label # LANG: Generic 'Reset' button label
nb.Button(conf_frame, text=_('Reset'), ttk.Button(conf_frame, text=_('Reset'),
command=lambda: coriolis_config.beta_textvar.set(value=DEFAULT_BETA_URL)).grid( command=lambda: coriolis_config.beta_textvar.set(value=DEFAULT_BETA_URL)).grid(
sticky=tk.W, row=cur_row, column=2, padx=PADX, pady=0 sticky=tk.W, row=cur_row, column=2, padx=PADX, pady=0
) )

View File

@ -113,10 +113,10 @@ class This:
self.cmdr_text: nb.Label | None = None self.cmdr_text: nb.Label | None = None
self.user_label: nb.Label | None = None self.user_label: nb.Label | None = None
self.user: nb.Entry | None = None self.user: ttk.Entry | None = None
self.apikey_label: nb.Label | None = None self.apikey_label: nb.Label | None = None
self.apikey: nb.Entry | None = None self.apikey: ttk.Entry | None = None
this = This() this = This()
@ -345,14 +345,14 @@ def plugin_prefs(parent: ttk.Notebook, cmdr: str | None, is_beta: bool) -> tk.Fr
# LANG: EDSM Commander name label in EDSM settings # LANG: EDSM Commander name label in EDSM settings
this.user_label = nb.Label(frame, text=_('Commander Name')) this.user_label = nb.Label(frame, text=_('Commander Name'))
this.user_label.grid(row=cur_row, padx=PADX, pady=PADY, sticky=tk.W) this.user_label.grid(row=cur_row, padx=PADX, pady=PADY, sticky=tk.W)
this.user = nb.Entry(frame) this.user = ttk.Entry(frame)
this.user.grid(row=cur_row, column=1, padx=PADX, pady=BOXY, sticky=tk.EW) this.user.grid(row=cur_row, column=1, padx=PADX, pady=BOXY, sticky=tk.EW)
cur_row += 1 cur_row += 1
# LANG: EDSM API key label # LANG: EDSM API key label
this.apikey_label = nb.Label(frame, text=_('API Key')) this.apikey_label = nb.Label(frame, text=_('API Key'))
this.apikey_label.grid(row=cur_row, padx=PADX, pady=PADY, sticky=tk.W) this.apikey_label.grid(row=cur_row, padx=PADX, pady=PADY, sticky=tk.W)
this.apikey = nb.Entry(frame, show="*", width=50) this.apikey = ttk.Entry(frame, show="*", width=50)
this.apikey.grid(row=cur_row, column=1, padx=PADX, pady=BOXY, sticky=tk.EW) this.apikey.grid(row=cur_row, column=1, padx=PADX, pady=BOXY, sticky=tk.EW)
cur_row += 1 cur_row += 1

View File

@ -125,7 +125,7 @@ class This:
self.log: 'tk.IntVar' self.log: 'tk.IntVar'
self.log_button: nb.Checkbutton self.log_button: nb.Checkbutton
self.label: HyperlinkLabel self.label: HyperlinkLabel
self.apikey: nb.Entry self.apikey: ttk.Entry
self.apikey_label: tk.Label self.apikey_label: tk.Label
self.events: dict[Credentials, Deque[Event]] = defaultdict(deque) self.events: dict[Credentials, Deque[Event]] = defaultdict(deque)
@ -292,7 +292,7 @@ def plugin_prefs(parent: ttk.Notebook, cmdr: str, is_beta: bool) -> tk.Frame:
# LANG: Inara API key label # LANG: Inara API key label
this.apikey_label = nb.Label(frame, text=_('API Key')) # Inara setting this.apikey_label = nb.Label(frame, text=_('API Key')) # Inara setting
this.apikey_label.grid(row=cur_row, padx=PADX, pady=PADY, sticky=tk.W) this.apikey_label.grid(row=cur_row, padx=PADX, pady=PADY, sticky=tk.W)
this.apikey = nb.Entry(frame, show="*", width=50) this.apikey = ttk.Entry(frame, show="*", width=50)
this.apikey.grid(row=cur_row, column=1, padx=PADX, pady=BOXY, sticky=tk.EW) this.apikey.grid(row=cur_row, column=1, padx=PADX, pady=BOXY, sticky=tk.EW)
cur_row += 1 cur_row += 1

View File

@ -361,12 +361,12 @@ class PreferencesDialog(tk.Toplevel):
# Type ignored due to incorrect type annotation. a 2 tuple does padding for each side # Type ignored due to incorrect type annotation. a 2 tuple does padding for each side
self.outdir_label.grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=row.get()) # type: ignore self.outdir_label.grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=row.get()) # type: ignore
self.outdir_entry = nb.Entry(output_frame, takefocus=False) self.outdir_entry = ttk.Entry(output_frame, takefocus=False)
self.outdir_entry.grid(columnspan=2, padx=self.PADX, pady=self.BOXY, sticky=tk.EW, row=row.get()) self.outdir_entry.grid(columnspan=2, padx=self.PADX, pady=self.BOXY, sticky=tk.EW, row=row.get())
text = (_('Browse...')) # LANG: NOT-macOS Settings - files location selection button text = (_('Browse...')) # LANG: NOT-macOS Settings - files location selection button
self.outbutton = nb.Button( self.outbutton = ttk.Button(
output_frame, output_frame,
text=text, text=text,
# Technically this is different from the label in Settings > Output, as *this* is used # Technically this is different from the label in Settings > Output, as *this* is used
@ -399,7 +399,7 @@ class PreferencesDialog(tk.Toplevel):
logdir = default logdir = default
self.logdir.set(logdir) self.logdir.set(logdir)
self.logdir_entry = nb.Entry(config_frame, takefocus=False) self.logdir_entry = ttk.Entry(config_frame, takefocus=False)
# Location of the Journal files # Location of the Journal files
nb.Label( nb.Label(
@ -413,7 +413,7 @@ class PreferencesDialog(tk.Toplevel):
text = (_('Browse...')) # LANG: NOT-macOS Setting - files location selection button text = (_('Browse...')) # LANG: NOT-macOS Setting - files location selection button
with row as cur_row: with row as cur_row:
self.logbutton = nb.Button( self.logbutton = ttk.Button(
config_frame, config_frame,
text=text, text=text,
# LANG: Settings > Configuration - Label for Journal files location # LANG: Settings > Configuration - Label for Journal files location
@ -423,7 +423,7 @@ class PreferencesDialog(tk.Toplevel):
if config.default_journal_dir_path: if config.default_journal_dir_path:
# Appearance theme and language setting # Appearance theme and language setting
nb.Button( ttk.Button(
config_frame, config_frame,
# LANG: Settings > Configuration - Label on 'reset journal files location to default' button # LANG: Settings > Configuration - Label on 'reset journal files location to default' button
text=_('Default'), text=_('Default'),
@ -465,7 +465,7 @@ class PreferencesDialog(tk.Toplevel):
text=_('Hotkey') # LANG: Hotkey/Shortcut settings prompt on Windows text=_('Hotkey') # LANG: Hotkey/Shortcut settings prompt on Windows
).grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=cur_row) ).grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=cur_row)
self.hotkey_text = nb.Entry(config_frame, width=30, justify=tk.CENTER) self.hotkey_text = ttk.Entry(config_frame, width=30, justify=tk.CENTER)
self.hotkey_text.insert( self.hotkey_text.insert(
0, 0,
# No hotkey/shortcut currently defined # No hotkey/shortcut currently defined
@ -623,7 +623,7 @@ class PreferencesDialog(tk.Toplevel):
self.loglevel_dropdown.configure(width=15) self.loglevel_dropdown.configure(width=15)
self.loglevel_dropdown.grid(column=1, pady=self.BOXY, sticky=tk.W, row=cur_row) self.loglevel_dropdown.grid(column=1, pady=self.BOXY, sticky=tk.W, row=cur_row)
nb.Button( ttk.Button(
config_frame, config_frame,
# LANG: Label on button used to open a filesystem folder # LANG: Label on button used to open a filesystem folder
text=_('Open Log Folder'), # Button that opens a folder in Explorer/Finder text=_('Open Log Folder'), # Button that opens a folder in Explorer/Finder
@ -726,7 +726,7 @@ class PreferencesDialog(tk.Toplevel):
self.theme_label_0.grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=cur_row) self.theme_label_0.grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=cur_row)
# Main window # Main window
self.theme_button_0 = nb.ColoredButton( self.theme_button_0 = tk.Button(
appearance_frame, appearance_frame,
# LANG: Appearance - Example 'Normal' text # LANG: Appearance - Example 'Normal' text
text=_('Station'), text=_('Station'),
@ -739,7 +739,7 @@ class PreferencesDialog(tk.Toplevel):
with row as cur_row: with row as cur_row:
self.theme_label_1 = nb.Label(appearance_frame, text=self.theme_prompts[1]) self.theme_label_1 = nb.Label(appearance_frame, text=self.theme_prompts[1])
self.theme_label_1.grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=cur_row) self.theme_label_1.grid(padx=self.PADX, pady=self.PADY, sticky=tk.W, row=cur_row)
self.theme_button_1 = nb.ColoredButton( self.theme_button_1 = tk.Button(
appearance_frame, appearance_frame,
text=' Hutton Orbital ', # Do not translate text=' Hutton Orbital ', # Do not translate
background='grey4', background='grey4',
@ -870,7 +870,7 @@ class PreferencesDialog(tk.Toplevel):
padx=self.PADX, pady=self.PADY, sticky=tk.W, row=row.get() padx=self.PADX, pady=self.PADY, sticky=tk.W, row=row.get()
) )
plugdirentry = nb.Entry(plugins_frame, justify=tk.LEFT) plugdirentry = ttk.Entry(plugins_frame, justify=tk.LEFT)
self.displaypath(plugdir, plugdirentry) self.displaypath(plugdir, plugdirentry)
plugdirentry.grid(columnspan=2, padx=self.PADX, pady=self.BOXY, sticky=tk.EW, row=row.get()) plugdirentry.grid(columnspan=2, padx=self.PADX, pady=self.BOXY, sticky=tk.EW, row=row.get())
@ -882,7 +882,7 @@ class PreferencesDialog(tk.Toplevel):
text=_("Tip: You can disable a plugin by{CR}adding '{EXT}' to its folder name").format(EXT='.disabled') text=_("Tip: You can disable a plugin by{CR}adding '{EXT}' to its folder name").format(EXT='.disabled')
).grid(columnspan=2, padx=self.PADX, pady=self.PADY, sticky=tk.EW, row=cur_row) ).grid(columnspan=2, padx=self.PADX, pady=self.PADY, sticky=tk.EW, row=cur_row)
nb.Button( ttk.Button(
plugins_frame, plugins_frame,
# LANG: Label on button used to open a filesystem folder # LANG: Label on button used to open a filesystem folder
text=_('Open'), # Button that opens a folder in Explorer/Finder text=_('Open'), # Button that opens a folder in Explorer/Finder