FE 'tkGooie' Utilities

'SELECTORtools' group

tkSpeedGun

(Speed Converter
and Selector)

(FE = Freedom Environment)

GUI interface to convert
tennis ball speeds between
kilometers-per-hour and
miles-per-hour and
'other'.
---
A tiny-to-monster
(micro-to-macro) speed
conversion utility is
presented on
another page.

FE Home Page > FE Downloads Page >

FE 'tkGooies' Description Page >

FE 'tkGooies' 'SELECTORtools' Menu >

This
'tkSpeedGun_ VelocityConvertSelect'
tkGooie code Page

INTRODUCTION to Tcl-Tk script
'tkSpeedGun_VelocityConvertSelect'

In 2013, I posted an angle selector utility on wiki.tcl.tk --- at a page titled

"tkAngle-Convert-Select -
radians, degrees,
percents-of-circumference-of-circle"
.

I based that GUI on my color-selector GUI (with 3 'slider buttons') --- whose code is provided on this FE web site on a page titled

'A non-obfuscated color selector GUI'.

These GUI's set me up for the following.

I watch a lot of tennis.

The various tournaments around the world usually have a speed gun that measures the speed of serves --- and occasionally the speedgun picks up the speed of ground strokes.

In some countries, the speed guns report the speed in kilometers per hour --- in other countries, in miles per hour.

Some tennis announcers pride themselves on being able to convert quickly from kph to mph and vice versa.

I don't do the conversion frequently enough to make it worth committing a technique to memory --- BUT I realized that I could easily make a 'tkSpeedGun' conversion utility --- based on the code I used for the 'tkAngle-Convert-Select' utility.

Like the 'angle converter' GUI, for the 'speed converter' GUI, the 3 sliders would need to move together.

The Tk 'scale' widget handles that nicely.

In Tk, a variable is associated with each 'slider', and any time the value of that variable changes, the slider moves to reflect the changed value.

I just needed to make sure that when any of the 3 variables (containing the value of a speed in 3 different units) changed, the other 2 variables were changed correspondingly.

---

No Canvas on This One:

In addition to the 3 'scale' widgets, on the 'color selector' utility, there was a 'color swatch' (which was done with a 'frame' widget) whose color changed as any of the 3 scales were changed.

For the 'angle selector' utility, I used a 'canvas' widget, on which a representation of the current angle could be drawn --- as a circle and a filled-arc on the circle.

For this 'speed selector' utility, I was not imaginative enough to think of something to draw on a 'canvas' widget to represent the current speed settings.

So there is no canvas on this 'speed gun' GUI.

    (I suppose I could have used a yellow disk representing a tennis ball --- and changed its size --- its radius --- according to the current speed setting of the 'scale' widgets.)

---

Some Fun with the 3rd Speed:

I knew I wanted to use kilometers/hour and miles/hour for 2 of the scales on the GUI.

I decided to have a little fun with the 3rd scale and have it present the speed of the other 2 scales in a rather unusual unit of measure.

I thought of light-years for the distance, and needed to figure out what time-unit gave a value somewhat in the realm of comprehension --- i.e. not some huge number involving, say, 10 to the twelfth.

It turns out that a billion years gives some almost-mentally-digestable units --- even though the LPB (Light-years Per Billion-years) units are in themselves out of the realm of everyday experience.

But here was a chance to show LPB speed values that correspond to everyday KPH and MPH values.

So I set to work converting the code of the angle-converter-selector GUI to a 'tkSpeedGun' GUI, and I ended up with the GUI seen in the following image.


These colors are a bit garish.
You can easily change them,
in a color-variables section of the code.

Whenever ONE of the 'sliders' is moved with mouse-button1 :

  • the other TWO sliders move (showing the converted values)

  • the 3 text areas, at the top of the GUI, change to present the current 3 values for the speed

    The 3 text areas make the 3 numbers available for copy-paste into another window on the desktop, such as a text-editor window.

The new values show as soon as mouse-button1 is released.


The 'Help' button on the GUI provides pretty complete and detailed help for using the GUI.

Here is most of the Help that is in the 'HELPtext' variable:



   *** HELP for this tkSpeedGun-Convert-Select GUI ****
   
   This TkGUI script provides a GUI for showing speeds
   between zero and 2,000 kilometers-per-hour (this max
   is easy to change) --- in three different units:
   
      - mph (miles-per-hour)
      - kph (kilometers-per-hour)
      - LPB (Lightyears-Per-Billion-years).
   
   
   *******************************
   SOME NOTEWORTHY SPEEDS (in kph):
   
   speed of light in a vacuum = 1.07925285 billion kph
   
                              = 1,079,252,850 kph
   
   speed of sound in air at sea level = 1,225.044 kph
   
                                      = 343.2 meters/second
   
   (And sound travels about 4.3 times faster in water,
   and nearly 15 times faster in iron.)

   terminal velocity of a skydiver  = about 200 kph
   
   speed limit of 80 mph            =  128.7 kph
   
   max (burst) speed of a cheetah   = about 112 kph
   
   speed limit of 60 mph            =  96.6 kph
   
   max speed of a horse             = about 88 kph
   
   human walking speed of 4 mph     =  6.4 kph
   
   
   ****************************************
   CONVERSION FACTORS BETWEEN 'LPB' and KPH:
   
   1 light-year   = 9.4605284  10^12 kilometers
                  = 9.46 trillion kilometers
   
   1 billion years = ? hours
                   = 10^9 years * 365 days/year * 24 hours/day
                   = 8.760 x 10^12 hours
                   = 8.76 trillion hours
   
   So

   1 light-year/billion-years (LPB) =  1.0799 kph
   
   or
   
   1 kph =  0.9260 LPB
   
   So multiply kph by 0.926 to get LPB.
   
   
   ****************************
   THE CONVERSION FUNCTIONALITY:
   
   Three different (but corresponding) speeds are shown (and
   selected for conversion) via 3 'slider-buttons' on 3 Tk
   'scale' widgets.
   
   These 3 different (corresponding) speed numbers are also shown
   in 3 'text' widgets, several characters wide --- from which a
   user can copy-and-paste the numbers from this GUI window to
   some other window.
   
   For any 'slider' change, this GUI script IMMEDIATELY updates
   the location of the other 2 'SLIDERS' --- when you RELEASE
   the slider-button that you are moving.

   The 3 TEXT widgets, showing the speed in the 3 different
   units, are also IMMEDIATELY updated whenever any one of the 3
   scale widget 'sliders' are changed.
   
   ***************************
   FINE CONTROL OF THE SLIDERS:
   
   Most people know that you can drag the sliders by clicking
   on the 'slider button' with mouse-button1 and dragging the
   slider button. This is a rather coarse way of moving the
   slider button.
   
   It is not so obvious that the Tk scale widget also allows
   you to move the 'slider button' by clicking in the 'trough'
   on EITHER SIDE of the slider button. By repeated clicking
   in the trough, the slider can be advanced one scale-resolution
   unit per click.
   
   Furthermore, one can RAPIDLY move the slider button ONE
   RESOLUTION UNIT per 'auto-click' by clicking in the trough
   and HOLDING down the mouse button. The slider moves
   one resolution unit repeatedly UNTIL the mouse button
   is RELEASED.

   **************************
   THE SELECTOR FUNCTIONALITY:
   
   By clicking on the 'UseIt' button on the GUI, this script
   returns a string containing the current speed (in the
   3 different units) to a calling script/application.
   
   When using this script in another script/program,
   this script will accept a speed (in kph) as an
   argument --- which is used to initialize the
         - setting of the 3 'sliders', and
         - the numbers shown in the 3 small text widgets.
   
   *************************
   SOME USES of this utility:

   1) Useful for helping math-science students get a visual
      'feel' for the relation between mph, kph, and LPB.

   2) Also could be useful for a person wanting to convert
      from one unit to another --- for example, a tennis serve
      in miles-per-hour to kilometers-per-hour --- or vice versa.

   3) IN A SHELL SCRIPT or ANOTHER TK SCRIPT, this Tk script can
      ACT AS AN SPEED SELECTOR by passing the current speed
      (in the 3 different units) to stdout, when the 'UseIt' button  
      is clicked.
            Example output string:  60.0  96.6  89.5

                                    (mph , kph , LPB)

   Note:
   The user can set up this script as an icon on a desktop so that
   the GUI can be started up by a click (or two) on the icon ---
   say, while you are watching a tennis tournament.


DESCRIPTION OF THE CODE

Below, I provide the Tk script code for this 'tkSpeedGun-Convert-Select' utility.

I follow my usual 'canonical' structure for Tk code for this Tk script:



  0) Set general window & widget parms (win-name, win-position,
     win-color-scheme, fonts, widget-geometry-parms, win-size-control,
     text-array-for-labels-etc).

  1a) Define ALL frames (and sub-frames, if any).
  1b) Pack   ALL frames and sub-frames.

  2) Define & pack all widgets in the frames, frame by frame.
              Within each frame, define ALL the widgets.
              Then pack the widgets.

  3) Define keyboard and mouse/touchpad/touch-sensitive-screen action
     BINDINGS, if needed.

  4) Define PROCS, if needed.

  5) Additional GUI initialization (typically with one or more of
     the procs), if needed.


This Tk coding structure is discussed in more detail on the page A Canonical Structure for Tk Code --- and variations.

This structure makes it easy for me to find code sections --- while generating and testing a Tk script, and when looking for code snippets to include in other scripts (code re-use).

I call your attention to step-zero.

One new thing that I have started doing recently is using a text-array variable --- named 'aRtext' --- for text in labels, buttons, and other widgets in the GUI.

This can make it easier for people to internationalize my scripts.

I will be using a text-array like this in most of my scripts in the future.


Experimenting with the GUI
--- its behavior:

As in all my scripts that use the 'pack' geometry manager (which is all of my 100-plus scripts, so far), I provide the four main 'pack' parameters:

  • '-side'
  • '-anchor'
  • '-fill'
  • '-expand'

on all of the 'pack' commands for the frames and widgets.

That helps me when I am initially testing the behavior of a GUI (the various widgets within it) as I resize the main window.

In this particular GUI, like for the 'color selector' GUI on which it was based, I have chosen to make the window stay at the same size that it takes when it is initially 'packed'.

However, one could comment the statement

wm   resizable   .   0   0

and let the statements that set the min-width and min-height values take effect, along with

wm   minsize   .   $minWinWidthPx   $minWinHeightPx

If you want to make the window resizable, you can experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various frames and widgets --- to get the widget behavior that you want.

... and think of a nice, re-sizing canvas widget to add, with some appropriate dynamic image placed upon it.

---

Additional experimentation with the GUI
--- its appearance:

You might want to change the fonts used for the various GUI widgets.

For example, you could change '-weight' from 'bold' to 'normal' --- or '-slant' from 'roman' to 'italic'.

Or change font families.

    In fact, you may NEED to change the font families, because the families I used may not be available on your computer --- and the default font that the 'wish' interpreter chooses may not be very pleasing.

I use variables to set geometry parameters of widgets --- parameters such as border-widths and padding.

And I have included the '-relief' parameter on the definitions of frames and widgets.

Feel free to experiment with those 'appearance' parameters as well.


Some features in the code

There are plenty of comments in the code, to describe what most of the code-sections are doing.

You can look at the top of the PROCS section of the code to see a list of the procs used in this script, along with brief descriptions of how they are called and what they do.

The main procs are as follows.



   'speed_update'            - called once initially, and then by button1-release bindings on the scale widgets

   'put_vars'                - called by the 'UseIt' button, to return the 3 speed values to stdout

   'popup_msgVarWithScroll'  - called by the 'Help' button


The main proc is the 'speed_update' proc.

As any of the 3 sliders is changed, it

  • sets the value for the other 2 sliders

  • updates the small text widgets that hold the speed in 3 different units.


Comments in the Code

It is my hope that the copious comments in the code will help Tcl-Tk coding 'newbies' get started in making GUI's like this.

Without the comments, potential young Tcler's might be tempted to return to their iPhones and iPads and iPods --- to watch videos of amazing tennis shots.

Potential Tclers:

When the tennis is over, try installing-running-enhancing the following Tcl-Tk code.


The Tcl-Tk script CODE

Here is a link to CODE for the script

'tkSpeedGun-Convert-Select_kph-mph-etc.tk'.


SOME POTENTIAL ENHANCEMENTS:

In looking up some sample speeds to include in the 'HELPtext' variable, I realized that often high speeds (like the speed of sound) are reported in meters/second as an alternative to kilometers/hour.

So I could have made the 3rd scale 'MPS' instead of 'LPB'.

In fact, there are other speed units that I might like to display, like Earth-circumferences Per Week (EPW) or Football-fields Per Minute (FPM).

So, I could add a row of RADIOBUTTONS (or a listbox) to the GUI, and allow the user to choose --- from quite a few weird and not-so-weird options --- what units will be used for the 3rd scale.

---

Furthermore, I probably should add a 'ColorMe' button to this GUI --- like I did for a font-selector GUI whose code is at wiki.tcl.tk in a page titled

"YAFSG - Yet Another Font Selector GUI"

and for a 'tkPointerSettings' GUI whose code I contributed at wiki.tcl.tk in a page titled

"tkPointerSettings - a Tk GUI 'wrapper'
for the 'xinput' command"
.

With a 'ColorMe' button, the user could easily use a color selector, like the one at the page titled

'A non-obfuscated color selector GUI',

to change the color of this tkSpeedGun-Convert-Select GUI.

Instead of supplying a 'ColorMe' button to do that, I simply point out that the user can change the RGB values for the 'tk_setPalette' statement near the top of this code.


IN CONCLUSION

The final weekend of the U.S. Open tennis tournament is this weekend.

So I have finished this utility just in time to give it a workout as Gasquet, Nadal, Wawrinka, and Djokovic battle it out.

Thanks again to Tcl-Tk --- that is, to Ousterhout and the maintainers of the 'wish' interpreter.

Bottom of this web page for
presenting Tcl-Tk code for
tkSpeedGun (Velocity Convert/Select)
--- a utility in the FE 'tkGooies' system,
in the 'SELECTORtools' group.

To return to a previously visited web page location, click on the Back button of your web browser a sufficient number of times. OR, use the History-list option of your web browser.
OR ...

< Go to Top of Page, above. >

Page history:

The code was created in 2013 --- and posted 2013 Sep 05 at http://wiki.tcl.tk/38666.

This FE web page was created 2014 May 17.
(as a backup and alternative to the wiki.tcl.tk page)

This page was changed 2015 Oct 05.
(Small changes.)

Page was changed 2019 Mar 07.
(Added css and javascript to try to handle text-size for smartphones, esp. in portrait orientation.)

Page was changed 2019 Jul 09.
(Specified image widths in percents to size the images according to width of the browser window. Also added some web links.)


NOTE:
The code here MAY BECOME more 'up-to-date' than the code posted on the Tcler's Wiki ---
wiki.tcl-lang.org --- formerly wiki.tcl.tk.