TkClassWizard (1.4.10)#
TkClassWizard - define objects graphically based on class annotations. The library allows users to create abstract “ObjectInfo” objects based on the class’s parameters, which can later be converted to real Python objects and vice versa.
Links#
Need help? Contact me in my Discord server.
Installation#
TkClassWizard can be installed though command prompt/terminal using the bottom commands.
Pre-requirement: Python (minimum v3.9)
pip install tkclasswiz
Example#
1import tkinter as tk
2import tkinter.ttk as ttk
3import tkclasswiz as wiz
4
5
6# Normal Python classes with annotations (type hints)
7class Wheel:
8 def __init__(self, diameter: float):
9 self.diameter = diameter
10
11class Car:
12 def __init__(self, name: str, speed: float, wheels: list[Wheel]):
13 self.name = name
14 self.speed = speed
15 self.wheels = wheels
16
17# Tkinter main window
18root = tk.Tk("Test")
19
20# Modified tkinter Combobox that will store actual objects instead of strings
21combo = wiz.ComboBoxObjects(root)
22combo.pack(fill=tk.X, padx=5)
23
24def make_car(old = None):
25 """
26 Function for opening a window either in new definition mode (old = None) or
27 edit mode (old != None)
28 """
29 assert old is None or isinstance(old, wiz.ObjectInfo)
30
31 window = wiz.ObjectEditWindow() # The object definition window / wizard
32 window.open_object_edit_frame(Car, combo, old_data=old) # Open the actual frame
33
34def print_defined():
35 data = combo.get()
36 data = wiz.convert_to_objects(data) # Convert any abstract ObjectInfo objects into actual Python objects
37 print(f"Object: {data}; Type: {type(data)}",) # Print the object and it's datatype
38
39
40# Main GUI structure
41ttk.Button(text="Define Car", command=make_car).pack()
42ttk.Button(text="Edit Car", command=lambda: make_car(combo.get())).pack()
43ttk.Button(text="Print defined", command=print_defined).pack()
44root.mainloop()