Newbie Help Please.(OO and Tkinter)

paul colby spam at eatshit.net
Mon Jun 17 02:10:31 EDT 2002


SA wrote:

> Hi Everyone-
> 
>     I've had some problems finishing this code. I think it stems from my
> lack of OOP understanding. Below is the code:
> 
> from Tkinter import *
> import os
> import commands
> import tkSimpleDialog
> 
> class PyShell(tkSimpleDialog.Dialog):
>     def clearin(self):
>         self.t1.delete(0.0,END)
>     def clearout(self):
>         self.t2.delete(0.0,END)
>     def expyth(self):
>         self.t2.delete(0.0, END)
>         self.output = commands.getoutput(self.t1.get(0.0, END))
>         self.t2.insert(END, self.output)
>     def __init__(self, top):
>         self.t1 = Text(top, height="12", width="84", font="Courier 12")
>         self.t1.pack(side=TOP, pady=2)
>         f = Frame(top)
>         f.pack()
>         self.b1 = Button(f, text="Execute", command=self.expyth)
>         self.b1.pack(side=LEFT)
>         self.b2 = Button(f, text="Clear Input", command=self.clearin)
>         self.b2.pack(side=LEFT)
>         self.b3 = Button(f, text="Clear Output", command=self.clearout)
>         self.b3.pack(side=LEFT)
>         self.b4 = Button(f, text="Save Input", command=Saving.savin)
>         self.b4.pack(side=LEFT)
>         self.t2 = Text(top, height="12", width="84", bg="lightblue",
> font="Courier 12")
>         self.t2.pack(side=TOP, pady=2)
>         
> class Saving(PyShell):
>     
>     def savin(self, question):
>         Label(question, text="Directory:").grid(row=0)
>         Label(question, text="Filename:").grid(row=1)
>         self.e1 = Entry(question)
>         self.e2 = Entry(question)
>         self.e1.grid(row=0, column=1)
>         self.e2.grid(row=1, column=1)
>         def apply(self):
>             self.dir = self.e1.get()
>             self.fn = self.e2.get()
>             self.sav = self.dir + self.fn
>             self.savfile = open(self.sav, "w")
>             for line in self.t1.readlines():
>                 self.savefile.write(line)
>             self.savefile.close()
>             
> root = Tk()
> app = PyShell(root)
> root.mainloop()
> 
> 
> 
> Everything runs until I try to save the text in t1 with the "Save Input"
> button. I end up with the following error:
> 
> Exception in Tkinter callback
> Traceback (most recent call last):
>   File "/sw/src/root-python-2.2.1-6/sw/lib/python2.2/lib-tk/Tkinter.py",
> line 1292, in __call__
>     return apply(self.func, args)
> TypeError: unbound method savin() must be called with Saving instance as
> first argument (got nothing instead)
> 
> What am I doing wrong? Saving should inherit from PyShell correct? And in
> order to work correctly saving needs tkSimpleDialog.Dialog, so PyShell
> inherits this correct?
> 
> Or am I totally wrong here?
> 
> Thanks.
> SA

When you set command=Saving.savin you are passing a member 
function which has no define instance pointer "self". Thus 
the "self" thingy in the definition of savin isn't there to 
be used when the button is pressed. Note that your other 
buttons likely work since you use constructs like 
command=self.clearin. Now I haven't tried it but I 
would expect if you included something like

        self.SAVE = Saving(self)

in the __init__ function definition for PyShell then you 
could use command=self.SAVE.savin for the Save Input button. 
Now when the button is pressed self = self.SAVE so there
is an instance.


Hope this helps

Regards
Paul Colby



More information about the Python-list mailing list