busted buttons, a common type of coding problem

Zipguy 2013-09-03 - You can find out my email address by clicking on Zipguy.

Coding Problem

I was writing a program for my own purposes, called RF (or Rename Files Files v0.61a), and I ran into a common type of mistake I've been making frequently.

What did I do wrong? Good question. I wrote (what I thought) was a simple line of buttons, within a frame. I thought it would be easy to put some on the left, the middle, and the right. It turned out harder than I thought. Here's what it looked like:

http://www.geocities.ws/thezipguy/misc/rf_busted_buttons.png

What did you do wrong?

Well there are lots of options, in fact, tons of options under buttons and pack. I was looking for the option I wanted, found a candidate, and added it. It did not work, because I'd picked the wrong option.

So I went back to the documentation, found another option, and tried it too.

What I should have done, is REMOVED the option I just added, that did not work.

It is a bad thing to have too many options, that don't do anything for you.

If you look at the code below, you'll see how I did try adding it to the other buttons also, hoping that that would work. Worse idea!

It seems way to simple, when it's done properly, although it may take a while to find the option, for which, you are looking.


So I just made a new rule for myself.

Go ahead and try an option, it kind of fun to see what happens with it. Try adding it to multiple items.

But when you've found that you don't like an option you've tried, remember all the place you put it in, and at least comment it out, or perhaps better, remove it.

I hope this situation, and my explanation, helps others.

 What did my code look like? It looked like this:
proc ren_files {  } {
#-----------------------------------------------------------------
# ren_files - allows you to rename a group of filenames (no extenstions)
#                   and you get to use regexp to rename them (and un-rename them)
#-----------------------------------------------------------------
        global _FR

        set _FR(status1)                                                        [pwd]
        set w .ren_files
        catch {destroy $w}
        toplevel $w
        wm title $w "$_FR(pgmNameFull), Rename...  Geetings from the ZipGuy!"

        set f                                                 $w.tb
        frame $f -relief raised -borderwidth 1
        pack $f\
                -side top -fill x                                                -pady 0m 

        button $f.preview\
                -text " Preview "\
                -command "preview"                                                -padx 0 -pady 0  
        pack $f.preview                                                        -side left -expand 1

        button $f.reset\
                -text " Reset "\
                -command "reset"                                                -padx 0 -pady 0  
        pack $f.reset                                                        -side left -expand 1

        button $f.dorename\
                -text " Do Rename! "\
                -command "dorename"                                                -padx 0 -pady 0 -fg {OliveDrab4} -bg gray99
# white sea green aquamarine indian red  
        pack $f.dorename                                                        -side left -expand 1

        button $f.undorename\
                -text " UnDo Rename "\
                -command "undorename"                                -padx 0 -pady 0 -fg {VioletRed3} -bg gray99
        pack $f.undorename                                                        -side left -expand 1

        button $f.exit1\
                -text "  Exit  "\
                -command "destroy $w"                                -padx 0 -pady 0  
        pack $f.exit1                                                        -side right -expand 1

        set q                                                 $w.mc
        frame $q -relief sunken -borderwidth 0 -width 450
        pack $q -side top                                                        -fill x -pady 0m
        label $q.s1       \
                -text "Above is messed up. ;(" \
                -anchor w                                                -justify left
        pack $q.s1 -side left                                                        -expand 1 -anchor w
        label $q.s2       \
                -text "And Below looks great! :)" \
                -anchor w                                                -justify right
        pack $q.s2 -side right                                                        -expand 1 -anchor e

        set f                                                 $w.tc
        frame $f -relief raised -borderwidth 1
        pack $f\
                -side top                                                 -fill x        -pady 0m 

        button $f.preview\
                -text " Preview "\
                -command "preview"
        pack $f.preview                                                        -side left

        button $f.reset\
                -text " Reset "\
                -command "reset"
        pack $f.reset                                                        -side left

        button $f.dorename\
                -text " Do Rename! "\
                -command "dorename"                                         -fg {OliveDrab4} -bg gray99
        pack $f.dorename                                                        -side left -expand 1

        button $f.undorename\
                -text " UnDo Rename "\
                -command "undorename"                                -fg {VioletRed3} -bg gray99
        pack $f.undorename                                                        -side left

        button $f.exit1\
                -text "  Exit  "\
                -command "destroy $w"
        pack $f.exit1                                                        -side right

        set f                                                 $w.text
        frame $f -relief raised -borderwidth 1 -width 700 
        pack $f                                \
                -side top -fill x -pady 0m -fill both -expand yes

        set fontspec1                                                "{{Courier new} 10 bold}"
        set fontspec2                                                "{{Courier} 10 normal}"

        label $f.s1       \
                -text "Cur. Directory: $_FR(status1)" \
                -anchor w                                                -justify left -font $fontspec1
        pack $f.s1 -side top                                                        -expand 0 -anchor w

        set f                                                 $w.text.2
        frame $f -relief sunken -borderwidth 0 -width 450
        pack $f -side top                                                        -fill x -pady 0m
        label $f.s1                   \
                -text "          Regexp: "          \
                -anchor w                                                -justify left  -font $fontspec1 
        pack $f.s1    -side left -expand 0 -anchor w -fill both
        entry $f.s1a                   \
                -textvariable _FR(regexp1) \
                                                                -justify left  -font $fontspec1
        pack $f.s1a   -side right -expand 1 -anchor w -fill both

#        add_files

        set w .ren_files


} ;# END-proc     


arjen - 2013-09-04 14:43:50

Usually when I want more control over the placement of the widgets, I use the grid geometry manager. Again loads of options you can try, but in my experience it is easier to find the right ones.