[Tutor] More Tkinter Help Please
SA
sarmstrong13@mac.com
Sat, 15 Jun 2002 20:57:03 -0500
On 6/15/02 1:56 AM, "Danny Yoo" <dyoo@hkn.eecs.berkeley.edu> wrote:
>
>
> On Fri, 14 Jun 2002 alan.gauld@bt.com wrote:
>
>>> class PyShell:
>>> def __init__(self, top):
>>> self.b1 = Button(f, text="Execute", command=self.expyth)
>>> self.b1.pack(side=LEFT)
>>>
>>> def expyth(self):
>>> output = os.popen("python -c").t1()
>>> self.t2.delete(0,END)
>>> sys.stdout.write.t2(output)
>>
>>> When I run this I get the following error:
>>>
>>> Traceback (most recent call last): AttributeError: PyShell instance
>>> has no attribute 'expyth'
>>
>> I think its because at the time that init is defined it can't see
>> self.expyth because its not been defined yet. Try moving the init
>> method after all the other methods?
>>
>>
>> However I'm not really happy with that answer because init shouldn't run
>> till you create an instance which is after you define the class, so I
>> could very well be wrong!
>
>
> Hi SA,
>
> Following up: did you find out what was causing this weird AttributeError?
> I'm just curious to know. *grin*
>
>
> Talk to you later!
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
Sorry. I've been busy and have not had a chance to reply yet.
Yes I was able to figure the issue. It had to do with cut and pasting
between a vi file and a bbedit file. I messed up my tabs. I rewrote the
whole program with the correct tabs and everything runs now.(with a few
other changes.)
I'm now working on the "save" button and am having a bit of trouble. I think
it stems from my lack of OO understanding. Here is what I have so far:
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 __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)
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):
dir = self.e1.get()
fn = self.e2.get()
sav = dir + fn
savfile = open(sav, "w")
for line in self.t1.readlines():
savefile.write(line)
osavefile.close()
root = Tk()
app = PyShell(root)
root.mainloop()
I think the problem is with:
class Saving(tkSimpleDialog.Dialog):
Since this is not a child of the PyShell class and therefore is not
inheriting? from this class? I think if I make PyShell inherit from
tkSimpleDialog.Dialog and Saving inherit from PyShell, this might work? What
do you think?
Thanks.
SA