[Tutor] Problems passing a parameter in a GUI

David Holland davholla2002 at yahoo.co.uk
Tue Jan 18 22:36:14 CET 2011


Hi All,
I hope this makes sense

I am trying to write a GUI where you click on a button and each time
you click on the button it shows in the entry text how many times you
have clicked.  However when I changed an earlier version of the code to
pass a parameter it errors.

This works :-



from Tkinter import *

class Application(Frame):

    """GUI App"""

    def __init__(self, master):

        """Initialize the frame."""

        Frame.__init__(self, master)

        self.grid()

        self.create_widgets()



    def create_widgets(self):

        self.inst_lbl=Label(self, text="Click on the button to get instructions")

        self.inst_lbl.grid(row=0, column=0, columnspan=2, sticky=W)

        self.pw_lbl=Label(self, text="Password")

        self.submit_bttn=Button(self, text="Submit", command=self.reveal)

        self.submit_bttn.grid(row=1, column=1, columnspan=2, sticky=W)

        self.secret_text=Text(self, width=35, height=5, wrap=WORD)

        self.secret_text.grid(row=2, column=1, columnspan=2, sticky=W)

        self.helpinfo()



    def reveal(self):

        Calmexob = Calmex()

        results = Calmexob.getmess()

        self.secret_text.delete(0.0, END)

        self.secret_text.insert(0.0,results)



    def helpinfo(self):

            self.secret_text.insert(0.0,"Start")



class Calmex(object):

    

    def __init__(self):

        """Default arg"""

        

    

    def getmess(self):

        mesg="Test"

        return mesg



#main

root=Tk()

root.title("Test")

root.geometry("400x300")

app=Application(root)

root.mainloop()



        

But when I changed it to use a number I get an error message.

Here is the code 

from Tkinter import *

class Application(Frame):

    """GUI App"""

    def __init__(self, master):

        """Initialize the frame."""

        Frame.__init__(self, master)

        self.grid()

        x=0

        self.create_widgets(x)



    def create_widgets(self,x):

        self.inst_lbl=Label(self, text="Click on the button to get instructions")

        self.inst_lbl.grid(row=0, column=0, columnspan=2, sticky=W)

        self.pw_lbl=Label(self, text="Password")

        x=   self.submit_bttn=Button(self, text="Submit", command=self.reveal(x))

        self.submit_bttn.grid(row=1, column=1, columnspan=2, sticky=W)

        self.secret_text=Text(self, width=35, height=5, wrap=WORD)

        self.secret_text.grid(row=2, column=1, columnspan=2, sticky=W)

        self.helpinfo()



    def reveal(self, x):

        Calmexob = Calmex(x)

        results = Calmexob.getmess(x)

        self.secret_text.delete(0.0, END)

        self.secret_text.insert(0.0,results)

        x=x+1

        return x



    def helpinfo(self):

            self.secret_text.insert(0.0,"Start")



class Calmex(object):

    

    def __init__(self,x):

        """Default arg"""

        

    

    def getmess(self,x):

        mesg="Test"+str(x)

        return mesg



#main

root=Tk()

root.title("Test")

root.geometry("400x300")

app=Application(root)

root.mainloop()



        

        







        

        



The error message is

AttributeError: Application instance has no attribute 'secret_text'



The only change is that I have declared x and changed the function   reveal to take and pass x as an arguement.



Thanks in advance.  I hope this makes sense.       








      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/ef04b236/attachment.html>


More information about the Tutor mailing list