Classes, OOP, Tkinter general comments and detailed questions...

Ron Stephens rdsteph at earthlink.net
Thu Apr 5 20:14:30 EDT 2001


I love Python. But I am having trouble getting to understand classes
well enough to use them. This is especially a problem because I want to
use Tkinter. I programmed a little in assembler, Basic, PL/1, and
Fortran in college (math and physics majors). Procedural programming
seems logical to me. Programs must follow the logic, and programs thus
can do only one thing. I am appealing for help from the group, seeking
ideas to help me find a way to understand the use of Python classes.

I have read the Guido tutorial  chapter (on the web) on classes three
times; I have also studied the online tutorial on Tkinter. I have read
the classes and OOP chapters of several different Python books. I just
can't believe that I am too stupid to understand classes but...

When I study a Tkinter program, it seems too "loose" or too "open-ended"
almost like it's not "determinate" enough. In other words, it sometimes
seems to me that the coder just made up some stuff that he felt might
work, and it did, almost magically. This bothers me. I hope someone can
give me a pointer or two on new avenues to pursue in my quest to learn
this. Maybe other readers could learn from such tips also; maybe I'm not
the only one who experiences such difficulties? Has anyone else had such
difficulties, only to finally triumph over them and learn Python OOP???
That would definitely inspire me to hear that...it would give me hope...

I am going to post a short Tkinter program that an acquaintance of mine
emailed to me; he was trying to show me how a "decision analysis" Python
program I wrote could be given a gui front end using Tkinter, something
I wanted to do. I can't reach him right now to ask questions
so...(perhaps some of you might recall my simple weighted average type
of "decision" program I posted on here, my first working Python program
;-)), well this is a gui Tkinter version sort of...)))

#! /usr/bin/env python

VERSION='0.1 GUI'
NUMCRITERIA=10
NUMOPTIONS=10

from Tkinter import *
import Pmw

class decision(Frame):
    def __init__(self,master):
        # initialize parent class
        Frame.__init__(self,master)

        # create list to hold widgets - 11 rows of 10 widgets
        self.buttons=[]
        self.strings=[]
        for x in range(NUMOPTIONS+2):
        # can't just create the entire list (results in the inner lists
being identical
        # which leads to losing widgets
            self.buttons.append([None]*(NUMCRITERIA+1))
            self.strings.append([None]*NUMCRITERIA)
        self.score=[None]*NUMOPTIONS
        self.master=master

        # create the Criteria Titles - as entry fields so you can set
the value.
        for x in range(NUMCRITERIA):
            self.strings[0][x]=StringVar()

self.buttons[0][x]=Entry(self,bg="white",width=8,textvariable=self.strings[0][x],justify=LEFT)

            self.buttons[0][x].grid(row=0,col=x,sticky=NSEW)
     self.grid_columnconfigure(x,weight=1)
 # create the score column title - as button to activate scoring

Button(self,text="Score",padx=1,justify=LEFT,command=self.scoring).grid(row=0,col=NUMCRITERIA,sticky=NSEW)

        self.grid_columnconfigure(NUMCRITERIA,weight=1)

 # create the weighting line - as pull-down menu
        for x in range(NUMCRITERIA):
            self.strings[1][x]=StringVar()

self.buttons[1][x]=Pmw.OptionMenu(self,labelpos=W,menubutton_textvariable=self.strings[1][x],items=('1','2','3','4','5','6','7','8','9','10'),menubutton_width=8)

            self.buttons[1][x].grid(row=1,col=x,sticky=NSEW)

 # create the option lines - as entry fields
 for y in range(NUMOPTIONS):
            for x in range(NUMCRITERIA):
                self.strings[y+2][x]=StringVar()

self.buttons[y+2][x]=Entry(self,bg="white",width=8,textvariable=self.strings[y+2][x],justify=LEFT)

                self.buttons[y+2][x].grid(row=y+2,col=x,sticky=NSEW)
     # create the score column - as labels

self.buttons[y+2][NUMCRITERIA]=Label(self,text="",padx=1,justify=LEFT)

self.buttons[y+2][NUMCRITERIA].grid(row=y+2,col=NUMCRITERIA,sticky=NSEW)

    def scoring(self):
        print 'scoring'
        for y in range(NUMOPTIONS):
            self.score[y]=0
            for x in range(NUMCRITERIA):
                s=self.buttons[y+2][x].get()
                if s:
                    w=self.buttons[1][x].get()
                    if w:
                        self.score[y]=self.score[y]+(eval(s)*eval(w))

self.buttons[y+2][NUMCRITERIA].configure(text=str(self.score[y]))

        #self.update() # draw screen
        #self.after(REFRESH,self.redraw) # refresh after ? seconds


root=Tk()  # create the main window
# set the title
root.title("Decision Analysis Beta Test"+VERSION)
window=decision(root) # create the frame
window.pack(side=TOP,expand=YES,fill=BOTH) # place the frame in the main
window
root.mainloop()  # tk main loop


Now, I have some questions.

When he codes

self.buttons=[]
self.strings=[]

does he just make this up??? There are no defined methods called buttons
or strings that I now of, in the frame class. If he just makes them up,
what in the world do they mean????

When he says

self.buttons.append([None]*(NUMCRITERIA+1))
            self.strings.append([None]*NUMCRITERIA)
        self.score=[None]*NUMOPTIONS

what does ([None]*(NUMCRITERIA + 1)) mean??? How can None times anything
equal anything other than zero???
and is [None] a list ?????? I know, a dumb question....

These are not the only things I do not understand. But they are a start.
I really appreciate any help or comments. I feel dumb, but I would like
to know..

By the way, when I run the program, it fails to execute for me; giving
me the following error message..

scoring
Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python20\lib\lib-tk\Tkinter.py", line 1287, in __call__
    return apply(self.func, args)
  File "C:\Program Files\Python\decisionaaron.py", line 59, in scoring
    w=self.buttons[1][x].get()
AttributeError: 'OptionMenu' instance has no attribute 'get'

Any help truly and sorely appreciated....










More information about the Python-list mailing list