Skip to content

Commit 5b59ff0

Browse files
committed
Fixed convert_objects_to_script
Fixed enum flags Fixed tooltip Fixed union
1 parent b16bcc5 commit 5b59ff0

File tree

7 files changed

+42
-25
lines changed

7 files changed

+42
-25
lines changed

docs/source/changelog.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,26 @@ Glossary
1919
The change could break functionality from previous versions but only if it
2020
was used in a certain way.
2121

22+
|UNRELEASED|
23+
These changes have not been yet released.
2224

2325
---------------------
2426
Releases
2527
---------------------
2628

29+
v1.4.8
30+
=====================
31+
- Fixed :class:`tkclasswiz.convert.convert_objects_to_script` function:
32+
33+
- not being able to convert global enums (or enum flags)
34+
- duplicated import strings
35+
36+
- Fixed flags not definable as flags when editing list-like objects.
37+
- Fixed tooltip style not adjusting when using ttkbootstrap.
38+
E. g., in dark mode only the text was updated, but background was still the same.
39+
- Fixed the Python3.10+ Union type (``A | B``) not being detected as an union type.
40+
41+
2742
v1.4.7
2843
===================
2944
- Fixed incorrect item being removed (at incorrect index) when editing an object.

tkclasswiz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
SOFTWARE.
2828
"""
2929

30-
__version__ = "1.4.7"
30+
__version__ = "1.4.8"
3131

3232

3333
from .object_frame import *

tkclasswiz/annotations.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from inspect import isclass, isabstract
77
from itertools import product, chain
88
from contextlib import suppress
9+
from types import UnionType
910

1011
from .utilities import issubclass_noexcept
1112
from .doc import doc_category
@@ -155,12 +156,13 @@ def remove_classes(types: list):
155156
input_type.__name__ = origin.__name__
156157

157158
# Unpack Union items into a tuple
158-
if origin is Union or issubclass_noexcept(origin, (Iterable, Generic)):
159+
union_type = origin in {Union, UnionType}
160+
if union_type or issubclass_noexcept(origin, (Iterable, Generic)):
159161
new_types = []
160162
for arg_group in get_args(input_type):
161163
new_types.append(remove_classes(list(convert_types(arg_group))))
162164

163-
if origin is Union:
165+
if union_type:
164166
return tuple(chain.from_iterable(new_types)) # Just expand unions
165167

166168
# Process abstract classes and polymorphism

tkclasswiz/convert.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,11 @@ def convert_objects_to_script(object: Union[ObjectInfo, list, tuple, set, str]):
240240
object_str = f"{object.class_.__name__}(\n "
241241
attr_str = []
242242
for attr, value in object.data.items():
243-
if isinstance(value, (ObjectInfo, list, tuple, set, Enum)):
244-
value, import_data_ = convert_objects_to_script(value)
245-
import_data.extend(import_data_)
246-
247-
elif isinstance(value, str):
248-
value, _ = convert_objects_to_script(value)
249-
243+
value, import_data_ = convert_objects_to_script(value)
244+
import_data.extend(import_data_)
250245
attr_str.append(f"{attr}={value},\n")
251246

252247
import_data.append(f"from {object.class_.__module__} import {object.class_.__name__}")
253-
254248
object_str += " ".join(''.join(attr_str).splitlines(True)) + ")"
255249
object_data.append(object_str)
256250

@@ -265,8 +259,15 @@ def convert_objects_to_script(object: Union[ObjectInfo, list, tuple, set, str]):
265259
object_data.append(_list_data)
266260

267261
elif isinstance(object, Enum):
268-
import_data.append(f"from {object.__module__} import {type(object).__name__}")
269-
object_data.append(str(object))
262+
type_name = type(object).__name__
263+
import_data.append(f"from {object.__module__} import {type_name}")
264+
object_name = object._name_
265+
if not object_name:
266+
name = f"{type_name}({object._value_})"
267+
else:
268+
name = ' | '.join(map(lambda name: f"{type_name}.{name}", object_name.split('|')))
269+
270+
object_data.append(name)
270271

271272
else:
272273
if isinstance(object, str):
@@ -275,7 +276,7 @@ def convert_objects_to_script(object: Union[ObjectInfo, list, tuple, set, str]):
275276
else:
276277
object_data.append(str(object))
277278

278-
return ",".join(object_data).strip(), import_data
279+
return ",".join(object_data).strip(), sorted(set(import_data), key=lambda x: len(x), reverse=True)
279280

280281

281282
@extendable

tkclasswiz/object_frame/frame_iterable.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import get_args, get_origin, Any, List, Literal
2-
from inspect import isclass, isfunction
32
from functools import partial
4-
from enum import Enum
3+
from enum import Enum, Flag
54

65
from ..doc import doc_category
76
from ..deprecation import *
@@ -120,7 +119,7 @@ def _create_add_menu(self, args: list, menu: tk.Menu):
120119
if arg is None:
121120
insert_items.append(...)
122121
insert_items.append(None)
123-
elif issubclass_noexcept(arg, Enum):
122+
elif issubclass_noexcept(arg, Enum) and not issubclass_noexcept(arg, Flag):
124123
insert_items.append(...)
125124
insert_items.extend(list(arg))
126125
elif get_origin(arg) is Literal:

tkclasswiz/object_frame/frame_struct.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
from typing import get_args, get_origin, Iterable, Union, Literal, Dict, Tuple
2-
3-
from collections.abc import Iterable as ABCIterable
42
from functools import partial
53
from enum import Enum, Flag
64

@@ -252,7 +250,7 @@ def _fill_field_values(
252250
# The class of last list like type. Needed when "Edit selected" is used
253251
# since we don't know what type it was
254252
return any_filled
255-
253+
256254
@extendable
257255
def load(self, old_data: ObjectInfo):
258256
data = old_data.data

tkclasswiz/object_frame/tooltip.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import tkinter.ttk as ttk
22
import tkinter as tk
33

4+
from abc import ABCMeta, abstractmethod
45
from typing import Optional
56

67
from ..convert import ObjectInfo
78
from ..storage import *
8-
from ..dpi import dpi_scaled
9-
from abc import ABCMeta, abstractmethod
109

1110

1211
class BaseToolTip(tk.Toplevel):
@@ -24,12 +23,15 @@ def __init__(
2423
timeout_ms: int = 500,
2524
):
2625
super().__init__(widget)
27-
self.label = ttk.Label(self, background="white", wraplength=1000)
26+
ttk.Style().configure(
27+
style="tooltip.TLabel", # ttkbootstrap compatibility
28+
background="white",
29+
)
30+
self.label = ttk.Label(self, style="tooltip.TLabel", wraplength=1000)
2831
self.schedule_id = None
2932
self._widget = widget
3033
self.timeout_ms = timeout_ms
31-
self.label.pack(padx=5, pady=5)
32-
self.config(bg="white")
34+
self.label.pack()
3335
self.pack_propagate(True)
3436
self._hide_tooltip()
3537
self.overrideredirect(True)

0 commit comments

Comments
 (0)