libtextworker 0.1.4
Cross-platform, free and open library for Python projects
actionrow.py
1 """
2 @package libtextworker.interface.tk.actionrow
3 ActionRow class for Tkinter.
4 ActionRow is a vertical (layout) tkinter.Frame that is used for a specific action.
5 Useful for creating Settings pages.
6 """
7 
8 # A cross-platform library for Python apps.
9 # Copyright (C) 2023-2024 Le Bao Nguyen and contributors.
10 # This is a part of the libtextworker project.
11 # Licensed under the GNU General Public License version 3.0 or later.
12 
13 import tkinter
14 import tkinter.ttk
15 
16 class ActionRow(tkinter.Frame):
17  """
18  Inspired from libadwaita's ActionRow class,
19  tkActionRow is a vertical (layout) tkinter.Frame with
20  text on one side, and everything else like buttons on the other side.
21  All in one row.
22  """
23 
24  _curr_col: int = 0
25 
26  def PlaceObj(this, obj: tkinter.Misc, column: int | str = -1,
27  sticky: str = "e", *args, **kwds):
28  """
29  Place a widget using grid method.
30  @param obj (tkinter.Misc): What widget to place (class/function reference, NOT an instance)
31  @param column (int): Where to place (row is always 0). -1 will place the widget next to the last one.
32  @param sticky (str): Sticky option
33  @param args, kwds: Options for the widget to place
34  """
35 
36  if len(args) >= 1:
37  temp = list(args)
38  temp[0] = this
39  args = tuple(temp)
40  del temp
41  else:
42  kwds["master"] = this
43 
44  target = obj(*args, **kwds)
45  target.grid(column=column if column > -1 else this._curr_col + 1, row=0, sticky=sticky)
46  this._curr_col += 1
47  return target
48 
49  def PlaceObjPack(this, obj: tkinter.Misc, side: str = "right",
50  expand: bool = True, fill: str = "x",
51  *args, **kwds):
52  """
53  Place a widget using pack method.
54  @param obj (tkinter.Misc): What widget to place (class/function reference, NOT an instance)
55  @param side, expand, fill: Pack options
56  @param args, kwds: Options for the widget to place
57  """
58 
59  if len(args) >= 1:
60  temp = list(args)
61  temp[0] = this
62  args = tuple(temp)
63  del temp
64  else:
65  kwds["master"] = this
66 
67  target = obj(*args, **kwds)
68  target.pack(expand=expand, fill=fill, side=side)
69  return target
70 
71 if __name__ == "__main__":
72  app = tkinter.Tk()
73 
74  row = ActionRow(app)
75  row.PlaceObjPack(tkinter.Label, text="Hello world!", side="left")
76  row.PlaceObjPack(tkinter.ttk.Button, text="This was placed using pack() method")
77  row.pack(expand=False, fill="x")
78 
79  row2 = ActionRow(app)
80  row2.PlaceObjPack(tkinter.Label, text="Welcome to tkActionRow", side="left")
81  row2.PlaceObjPack(tkinter.ttk.Button, text="This was placed using pack() method")
82  row2.pack(expand=False, fill="x")
83 
84  app.mainloop()
def PlaceObjPack(this, tkinter.Misc obj, str side="right", bool expand=True, str fill="x", *args, **kwds)
Definition: actionrow.py:51
def PlaceObj(this, tkinter.Misc obj, int|str column=-1, str sticky="e", *args, **kwds)
Definition: actionrow.py:27