Redge Shepherd
TclTk Scripts

TclTk Scripts

MQTT-Broker Client Application - Part 5

Photo by David Pupaza on Unsplash

MQTT-Broker Client Application - Part 5

Tcl/Tk Text Widgets For Debugging and Errors

Redge Shepherd's photo
Redge Shepherd
·Jan 15, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

When developing an application from scratch, it is often helpful to know what your code is doing while it is running or if it is still running at all. Although we can run our application in debug mode, we may not capture all the details we're interested in seeing.

Two text boxes appear at the bottom of our application: Script Status and Error Status. I use the following "debug" and "errors" procedures to print to the Script Status and Error Status text boxes, respectively.

image.png

The procedures are the same, other than the path to the textbox receiving the text messages. The procedure ignores messages when the debugVerbose variable is false.

When the "debugVerbose" variable is true, and the textbox widget exists, the procedure appends the text message to the textbox. If the widget does not exist, the "puts" command sends the message to the console.

set debugVerbose true

proc debug { args } {
    if { $::debugVerbose } {
        if { [winfo exists .statusMessages.debugMessages.debug.debugMsgs] } {
                .statusMessages.debugMessages.debug.debugMsgs insert end "$args\n"
                .statusMessages.debugMessages.debug.debugMsgs see end-2c
        } else {
            puts "$args\n"
        }
    }
}

proc errors { args } {
    if { $::debugVerbose } {
        if { [winfo exists .statusMessages.errorMessages.errors.errorsMsgs] } {
                .statusMessages.errorMessages.errors.errorsMsgs insert end "$args\n"
                .statusMessages.errorMessages.errors.errorsMsgs see end-2c
        } else {
            puts "$args\n"
        }
    }
}

The above procedures display text in their respective windows or to the console depending on the value of debugVerbose. Of course, we can use variables to manage windows independently.

Once we have finished developing our application, we can limit the displayed messages without overwhelming the user.

Did you find this article valuable?

Support Redge Shepherd by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this