Skip to content

Commit

Permalink
fix: Fix the bug that function load_font can not take effect in som…
Browse files Browse the repository at this point in the history
…e cases under the Linux platform
  • Loading branch information
Xiaokang2022 committed Jan 8, 2025
1 parent ce365e1 commit 906f9e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 6 additions & 2 deletions tests/test_toolbox/test_utility.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pylint: disable=all

import contextlib
import importlib
import io
import pathlib
import platform
import tkinter
Expand Down Expand Up @@ -56,10 +58,12 @@ def test_embed_window(self) -> None:
self.assertIsNone(utility.embed_window(toplevel, self.tk))
toplevel.destroy()

@unittest.skipIf(platform.system() == "Linux", "???") # TODO
@unittest.skipIf(platform.system() == "Darwin", "This not work on Darwin.")
def test_load_font(self) -> None:
self.assertFalse(utility.load_font(""))
with io.StringIO() as captured_output:
with contextlib.redirect_stderr(captured_output):
self.assertFalse(utility.load_font(""))

path = pathlib.Path(__file__).parent.parent/"assets/fonts/FiraCode.ttf"
self.assertRaises(TypeError, utility.load_font, path)
self.assertTrue(utility.load_font(str(path)))
Expand Down
9 changes: 6 additions & 3 deletions tkintertools/toolbox/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import shutil
import tkinter
import tkinter.font
import traceback
import typing

from ..core import configs, virtual
Expand Down Expand Up @@ -139,7 +140,9 @@ def load_font(
return bool(min(num_fonts_added, 1))

if platform.system() == "Linux":
font_path = str(font_path)
if isinstance(font_path, bytes):
font_path = font_path.decode()

linux_fonts_dir = os.path.expanduser("~/.fonts/")

try:
Expand All @@ -150,8 +153,8 @@ def load_font(
atexit.register(os.remove, linux_fonts_dir + font_path.rsplit("/", 1)[-1])

return True
finally:
pass
except Exception as exc: # pylint: disable=W0718
traceback.print_exception(exc)

return False

Expand Down

0 comments on commit 906f9e3

Please sign in to comment.