libtextworker 0.1.4
Cross-platform, free and open library for Python projects
__init__.py
1 """
2 @package libtextworker.interface.base
3 @brief The base of all GUIs in libtextworker
4 """
5 
6 # A cross-platform library for Python apps.
7 # Copyright (C) 2023-2024 Le Bao Nguyen and contributors.
8 # This is a part of the libtextworker project.
9 # Licensed under the GNU General Public License version 3.0 or later.
10 
11 from enum import Flag, auto
12 from typing import Callable, Literal
13 
14 
15 class DC_FLAGS(Flag):
16  """
17  Flags for DirCtrl.
18  """
19 
20  DC_ONEROOT = auto() # Only one root to be allowed
21  DC_EDIT = auto() # Editable labels
22  DC_HIDEROOT = auto() # Hide root nodes
23  DC_MULTIPLE = auto() # Multiple selections
24  DC_DIRONLY = auto() # Show only directories
25  DC_RIGHTCL = auto() # Right click menu
26  DC_DYNAMIC = auto() # Watch for changes then refresh the widget itself
27  DC_USEICON = auto() # Use icons
28 
29 
30 class WidgetBase:
31  """
32  The base class for libtextworker GUI widgets.
33 
34  Use these variables before starting your work:
35  * Parent_ArgName: parent widget keyword in __init__'s kwds
36  * Styles: Widget styles, if able to use
37  * _Frame: Not all widgets require this. GUI toolkit's Frame widget.
38  If used, the actual widget will be placed into a frame.
39  __init__ function will return the (modified) args + kwds.
40  """
41 
42  Parent_ArgName: Literal["master", "parent", "Parent", "Master"] | str
43  Styles: auto
44  Frame: object | None = None
45  _Frame: Callable | None = None
46 
47  def __init__(this, *args, **kwds):
48  """
49  Usually this is called in WidgetBase-derived classes in order to modify
50  their args and kwds. The widget also will be placed into a frame if
51  _Frame is specified.
52  If not specified, do nothing.
53  """
54 
55  try:
56  # Get specific widget styles
57  if "w_styles" in kwds:
58  this.Styles = kwds["w_styles"]
59  kwds.pop("w_styles")
60 
61  # Try to place the actual widget into a frame
62  if this._Frame:
63  # Get the target parent widget
64  if not this.Parent_ArgName in kwds:
65  temp = list(args)
66  target_parent = temp[0]
67  temp.pop()
68  args = tuple(temp)
69  else:
70  target_parent = kwds[this.Parent_ArgName]
71  kwds.pop(this.Parent_ArgName)
72 
73  this.Frame = this._Frame(**{this.Parent_ArgName: target_parent})
74 
75  return args, kwds
76 
77  except:
78  return None
def __init__(this, *args, **kwds)
Definition: __init__.py:47