Hi Cristian,

  The problem is, self is only defined within the class definition.
Basically, the function are not part of the class, just called by the class.
We use a lambda to get the class passed to the function.
So, to correct your code:

from Tkinter import *

class MyApp:
   def __init__(self, parent):
       self.myParent = parent  ### (7) remember my parent, the root
       self.myContainer1 = Frame(parent)
       self.myContainer1.pack()

       self.button1 = Button(self.myContainer1)
       self.button1.configure(text="OK", background= "green")
       self.button1.pack(side=LEFT)
       self.button1.bind("<Button-1>", lambda e, win=self:
button1Click(win)) ### (1)

       self.button2 = Button(self.myContainer1)
       self.button2.configure(text="Cancel", background="red")
       self.button2.pack(side=RIGHT)
       self.button2.bind("<Button-1>", lambda e, win=self:
button2Click(win)) ### (2)

def button1Click(win):
   if win.button1["background"] == "green": ### (4)
       win.button1["background"] = "yellow"
   else:
       win.button2["background"] = "green"

def button2Click(win):
       win.myParent.destroy()     ### (6)


root = Tk()
myapp = MyApp(root)
root.mainloop()


root = Tk()
myapp = MyApp(root)
root.mainloop()




On Fri, Dec 17, 2010 at 4:35 AM, craf <p...@vtr.net> wrote:
> Hi.
>
> I'm testing this code:
>
> -------------------------------------------------------------------
> from Tkinter import *
>
> class MyApp:
>    def __init__(self, parent):
>        self.myParent = parent  ### (7) remember my parent, the root
>        self.myContainer1 = Frame(parent)
>        self.myContainer1.pack()
>
>        self.button1 = Button(self.myContainer1)
>        self.button1.configure(text="OK", background= "green")
>        self.button1.pack(side=LEFT)
>        self.button1.bind("<Button-1>", self.button1Click) ### (1)
>
>        self.button2 = Button(self.myContainer1)
>        self.button2.configure(text="Cancel", background="red")
>        self.button2.pack(side=RIGHT)
>        self.button2.bind("<Button-1>", self.button2Click) ### (2)
>
>    def button1Click(self, event):    ### (3)
>        if self.button1["background"] == "green": ### (4)
>            self.button1["background"] = "yellow"
>        else:
>            self.button2["background"] = "green"
>
>    def button2Click(self, event):  ### (5)
>        self.myParent.destroy()     ### (6)
>
>
> root = Tk()
> myapp = MyApp(root)
> root.mainloop()
>
> ---------------------------------------------------------------------
>
> Works without problems, but if I want to get the functions of the class,
> then the problems begin.
>
> from Tkinter import *
>
> class MyApp:
>    def __init__(self, parent):
>        self.myParent = parent  ### (7) remember my parent, the root
>        self.myContainer1 = Frame(parent)
>        self.myContainer1.pack()
>
>        self.button1 = Button(self.myContainer1)
>        self.button1.configure(text="OK", background= "green")
>        self.button1.pack(side=LEFT)
>        self.button1.bind("<Button-1>", self.button1Click) ### (1)
>
>        self.button2 = Button(self.myContainer1)
>        self.button2.configure(text="Cancel", background="red")
>        self.button2.pack(side=RIGHT)
>        self.button2.bind("<Button-1>", self.button2Click) ### (2)
>
> def button1Click(self, event):    ¡¡¡FUNCTION OUTSIDE THE CLASS!!!
>    if self.button1["background"] == "green": ### (4)
>        self.button1["background"] = "yellow"
>    else:
>        self.button2["background"] = "green"
>
> def button2Click(self, event): ¡¡¡FUNCTION OUTSIDE THE CLASS!!!
>        self.myParent.destroy()     ### (6)
>
>
> root = Tk()
> myapp = MyApp(root)
> root.mainloop()
>
> What would be the correct way to call them?
>
> Thanks in advance
>
> Regards
>
> Cristian Abarzua F
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss@python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to