Sunday, July 22, 2012

Tkinter Checkbutton Demo

This code is based on the Tcl check.tcl demo, modified to use ttk.Checkbutton. A ttk.Checkbutton does not have the same options as a standard tkinter checkbutton; in particular, the tristatevalue option referenced in the original demo is missing.

checkbutton $w.b0 -text "Safety Check" \
    -variable safety -relief flat \
    -onvalue "all" \
    -offvalue "none" \
    -tristatevalue "partial"

The option determines the value of a checkbutton when it is neither checked (onvalue) nor unchecked (offvalue); it corresponds to the alternate visual state for display purposes. As nothing prevents you from assigning an invalid value to a checkbutton I was able to indirectly mimic the behaviour found in the original demo (see _value_changed and _safety_changed methods in code below). The screenshot on the right shows how the Safety checkbutton's alternate state looks under Windows 7.



The original demo displays the checkbutton control variables in a separate window via a See Variables button. I've chosen to display them in a separate panel along side the checkbuttons themselves.

Saturday, July 21, 2012

Tkinter Button Demo

This code is based on the Tcl button.tcl demo, modified to use ttk.Button and its associated ttk.Style.

Four buttons are presented, the panel background is changed to match the selected button's colour name.


The original demo uses the option -highlightbackground  to change the button colour to match the selected background colour; ttk.Button does not have a highlightbackground option, instead, we need to change the ttk.Button style. To ensure that only the demo buttons are changed (and not all buttons), we clone the original button style, TButton, as Demo.TButton; assign it to each button in the demo panel, and then modify it.


On the left is the button appearance if the button background is not modified to match the selected demo panel background; on the right, the button`s appearance once it`s background has been set by modifying it`s associated style.

Tkinter Labels with Unicode

This demo is based on the Tcl unicodeout.tcl demo.


Friday, July 20, 2012

Tkinter Label Demo

This code is a conversion of the Tcl label.tcl Demo.

Pressing the See Code button opens up the Python source code file.


Tkinter demos

The tkinter demos that ship with the Python 3 install are in the tcl\8.5\demo directory, however; they are all tcl files! And well there are a number of decent tkinter reference sites available: TkDocs, tkinterbook, Tkinter 8.4 Reference, and A Tour of Tkinter Widgets, it's hard to find recent, full working examples of various things tkinter so I've decided to take a crack at converting the tcl demos myself; mainly as a learning experience but hopefully they'll be useful to others as well.

To begin with, the demos each appear in a window with three panels: a top panel describing the demo, a middle panel with the widgets being demonstrated and a bottom button panel.  As the top and bottom panels are essentially the same across the board, I've split the code into a separate file: demopanels.py. 

Also included is a CodeDialog class for the display of the demo's source code. It creates a modal dialog window with a scrolled text widget and cancel button.


This file will be imported into subsequent demos.

Saturday, May 19, 2012

Simplifying nested loops

I've recently been working my way through two Udacity courses, CS212, Design of Computer Programs, taught by Peter Norvig, and CS262, Programming Languages, taught by Westley Weimar and have become in interested in Functional Programming. So far, haven't found much reference material on the approach but I did come across some Charming Python articles that are very useful.

This is a re-work of one example from the first article, found under Eliminating Side-effects
# Examples of using functional programming in place of loops/control statements
# Reference: http://gnosis.cx/publish/programming/charming_python_13.html

# A larger example of converting IMPERATIVE program to FP
# Goal:
#   list pairs of numbers whose products are > 25
#
# Standard imperative approach, the 'more stuff' lines
# represent spots where other processing might occur in
# a full program and represent spots where the key variables
# may take on different values; as well, when this section
# of code is complete, all the variables may have values
# that may or not be expected, or wanted, in the remaining code

xs = (1,2,3,4)          # list of numbers
ys = (10, 15, 3, 22)    # second list of numbers
bigmuls = []            # list to hold results
# ... more stuff ....
for x in xs:
    for y in ys:
        # ... more stuff ....
        if x * y > 25:
            bigmuls.append( (x,y) )

print('Imperative Programming result:', bigmuls)            
            
# the same using an FP approach
#    bigmuls = lambda xs,ys: filter(lambda (x,y):x*y > 25, combine(xs,ys))
#    combine = lambda xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs)))
#    dupelms = lambda lst,n: reduce(lambda s,t:s+t, map(lambda l,n=n: [l]*n, lst))
#
# Note:
#    the methods given in the article can be reduced to one line
#    by using itertools.product() which produces 'all pairs' from two given lists
#    essentially, this was the purpose of the 'dupelms() and 'combine()' functions
#    in the original article. 
#    A key advantage of this approach:
#        the original variable value NEVER change, so no possible side-effects


from itertools import product

bigmuls = lambda xs,ys: filter(lambda pair: pair[0]*pair[1] > 25, product(xs,ys) )

print('Functional Programming result:', list(bigmuls(xs,ys)))

# Or an even better example, just use a 'list comprehension' as a one-off need

res = [a for a in product(xs,ys) if a[0]*a[1] > 25]
print('List Comprehension result    :', res)

Wednesday, May 9, 2012

Convert list of lists to single list

This example is based on a Udacity CS262 discussion forum post.

  messedUp = [['one'], ['two'],['three'], ['four']]
  return [item for inner in messedUp for item in inner]
  
  Result:   ['one', 'two', 'three', 'four']