First steps#

To define your first object parameters with TkClassWizard, you can create a tkinter.TopLevel inherited object ObjectEditWindow and then call its open_object_edit_frame() method. This will open up the window and then load tkinter.Frame, which will contain placeholders for all the parameter values.

The open_object_edit_frame() method accepts the following parameters:

  • class_: This is the class or function that will accept our given parameters.

  • return_widget: This is a widget that receives the value after saving the newly defined parameters.

  • old_data: The old_data GUI data.

  • check_parameters: Boolean parameter. If True, it will not test whether the object parameters are correct. When editing a function this is not ignored.

  • allow_save: Boolean parameter. If False, it will not allow the defined data to be saved; This also means it will be read-only.

Let’s take a look at an example.

 1import tkinter as tk
 2import tkinter.ttk as ttk
 3import tkclasswiz as wiz
 4
 5# Normal Python classes with annotations (type hints)
 6class Wheel:
 7    def __init__(self, diameter: float):
 8        self.diameter = diameter
 9
10class Car:
11    def __init__(self, name: str, speed: float, wheels: list[Wheel]):
12        self.name = name
13        self.speed = speed
14        self.wheels = wheels
15
16# Tkinter main window
17root = tk.Tk("Test")
18
19# Modified tkinter Combobox that will store actual objects instead of strings
20combo = wiz.ComboBoxObjects(root)
21combo.pack(fill=tk.X, padx=5)
22
23def make_car(old = None):
24    """
25    Function for opening a window either in new definition mode (old = None) or
26    edit mode (old != None)
27    """
28    assert old is None or isinstance(old, wiz.ObjectInfo)
29
30    window = wiz.ObjectEditWindow()  # The object definition window / wizard
31    window.open_object_edit_frame(Car, combo, old_data=old)  # Open the actual frame
32
33def print_defined():
34    data = combo.get()
35    data = wiz.convert_to_objects(data)  # Convert any abstract ObjectInfo objects into actual Python objects
36    print(f"Object: {data}; Type: {type(data)}",)  # Print the object and it's datatype
37
38
39# Main GUI structure
40ttk.Button(text="Define Car", command=make_car).pack()
41ttk.Button(text="Edit Car", command=lambda: make_car(combo.get())).pack()
42ttk.Button(text="Print defined", command=print_defined).pack()
43root.mainloop()

In this example we first import the library by typing import tkclasswiz as wiz. Then we define 2 classes, the class Wheel and class Car.

The Wheel class accepts a single parameter annotated with the float type. It is VERY IMPORTANT that all the parameters are annotated. Otherwise they will not be displayed when defining parameters through the GUI.

The Car class accepts the following parameters: name of type str, speed of type float and a list of wheels of type Wheel. The wheels parameter allows us to define multiple nested objects as well.

Then we create an instance of Tk, which is just the standard way for creating a tkinter app.

After that, we create a combo variable of type ComboBoxObjects, which will receive the Car object after it is defined successfully. However, it won’t receive an actual instance of Car. Instead, it will receive an abstract representation of the defined object. The abstract representation is an instance of tkclasswiz.convert.ObjectInfo and its job is to store the class (in our case Car) and the defined parameters. When displaying the defined abstract Car object inside the GUI, it will be displayed as Class(parameter1=value1, ...).

We then define 2 functions. The first one will open the definition window, while the second one will convert the abstract Car object into a real Python object.

The function make_car accepts a parameter old, which will be used to edit the existing object after we defined it at a later point. However, since it is not currently defined, it has no effect. The next lines of code in the function create the ObjectEditWindow definition window and load in the definition frame by calling the open_object_edit_frame(). With this method, we can pass the class of an object we want to define (Car), the return widget (combo) that receives the defined object, and the old_data parameter which would load in previously defined values (which currently don’t exist).

At the very bottom of the example, we define a few buttons:

  • ‘Define Car’: Calls the make_car function, opening the object definition window.

  • ‘Edit Car’: Calls the make_car function, opening the object definition window and loading in the already defined tkclasswiz.convert.ObjectInfo abstract Car object.

  • ‘Print defined’: Calls the print_defined function, which converts the abstract object into a real one and prints it out, including its type.

Now let’s take a look at how our example looks inside a GUI.