Newbie Help Please.(OO and Tkinter)

SA sarmstrong13 at mac.com
Mon Jun 17 16:17:45 EDT 2002


On 6/17/02 1:10 AM, in article
r3fP8.401$uH2.132 at newsread1.prod.itd.earthlink.net, "paul colby"
<spam at eatshit.net> wrote:
> 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.
> 
Well I gave this a shot and got the following error:

Traceback (most recent call last):
  File "PyShell.py", line 54, in ?
    app = PyShell(root)
  File "PyShell.py", line 18, in __init__
    self.SAVE = Saving()
TypeError: __init__() takes at least 2 arguments (1 given)

Here is the modified script:

from Tkinter import *
import os
import commands
import tkSimpleDialog

class PyShell:
    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 saving_callback():
        savin()    
    def __init__(self, top):
        self.SAVE = Saving(self)
        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=self.SAVE)
        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(tkSimpleDialog.Dialog):
    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()

Any ideas?

Thanks.
SA




More information about the Python-list mailing list