Error: 'Example' object has no attribute 'OnClick'

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Error: 'Example' object has no attribute 'OnClick'

himanshu
i want that when user select multiple buttons, it print all button's name. I have added all the buttons with there name in an array but don't know how to get its labeled name when user click this.

i have attached the code file

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

raman.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Error: 'Example' object has no attribute 'OnClick'

James Scholes
Your OnClick method is declared at the same level of indentation as the
code inside your loop, and thus it belongs to your InitUI method not
your frame.
--
James Scholes
http://twitter.com/JamesScholes

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Error: 'Example' object has no attribute 'OnClick'

firefox
In reply to this post by himanshu
import wx

class Example(wx.Frame):

    def __init__(self, parent, title):
       super(Example, self).__init__(parent, title = title,size =
(1000,800))

       self.InitUI()
       self.Centre()
       self.Show()

    def InitUI(self):
       
       p = wx.Panel(self)

       gs = wx.GridSizer(11, 18, 5, 1)

       A = [ "H", " "," "," "," "," "," "," "," "," "," "," "," "," ","
"," "," ", "He", "Li", "Be"," "," "," "," "," "," "," "," "," "," ", "B"
, "C", "N", "O", "F" , "Ne", "Na", "Mg"," "," "," "," "," "," "," ","
"," "," ", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca","Sc", "Ti", "V",
"Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br",
"Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru","Rh", "Pd", "Ag",
"Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "" , "Hf", "Ta",
"W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At",
"Rn", "Fr", "Ra", "", "Rf", "Db", "Sg", "Bh", "Hs","Mt", "Ds", "Rg",
"Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og",
" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ", " ", "
"," ", " ", " ", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb",
"Dy", "Ho", "Er", "Tm","Yb", "Lu"," ", " ", " ","Ac", "Th" , "Pa", "U",
"Np", "Pu", "Am", "Cm", "Bk","Cf", "Es", "Fm", "Md" , "No", "Lr"," ","
"," "," "," "," "," "," "," " ,"Go"," "," "," "," "," "," "," "," " ]
       for i in A:
        btn = str(i)
        a = wx.Button(self, 10, "str(i)", (20,20)) #Buttons are added
        a.myname = "str(i)"
        #self.Bind(wx.EVT_BUTTON, self.OnClick, a)
  ##  AttributeError: 'Example' object has no attribute 'OnClick'
        print(i)

        def OnClick(self, event):  #When the button is clicked
            name = event.GetEventObject().myname

        p.SetSizer(gs)

app = wx.App()
Example(None, title = 'Grid demo')
app.MainLoop()


'''




'''



--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Error: 'Example' object has no attribute 'OnClick'

Karsten Hilbert
On Wed, Jun 14, 2017 at 01:18:08PM +0200, [hidden email] wrote:

>        #self.Bind(wx.EVT_BUTTON, self.OnClick, a)  ##  AttributeError: 'Example' object has no attribute 'OnClick'

Python is sensitive to whitespace.

Karsten
--
GPG key ID E4071346 @ eu.pool.sks-keyservers.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

how to get its labeled name

firefox
In reply to this post by himanshu
On 01.06.2017 11:03, Himanshu Pareek wrote:
> i want that when user select multiple buttons, it print all button's
> name. I have added all the buttons with there name in an array but don't
> know how to get its labeled name when user click this.


well, in Boa Constructor + PyCrust  it is well doable. Want the code ?

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Error: 'Example' object has no attribute 'OnClick'

James Scholes
In reply to this post by firefox
We've been through this before.  Your __init__ and InitUI instance
methods are defined at a 3-space level of whitespace.  Your OnClick
handler is defined with 7 spaces, so it doesn't belong to your wx.Frame
instance, it belongs to your for loop.  This is not what you want.
--
James Scholes
http://twitter.com/JamesScholes

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

this works... 3 buttons with on-click bound

firefox
import wx

global p , gs

class Example(wx.Frame):
     def __init__(self, parent, title):
         super(Example, self).__init__(parent, title = title,size =
(500,300))
         self.InitUI()
         self.Centre()
         self.Show()

     def InitUI(self):
         global p , gs
         p  = wx.Panel(self)
         gs = wx.GridSizer(11, 18, 5, 1)
         A  = [ "He1", "He2 ","Ar3 " ]  # put more in if u want
         ii = 1
         for i in A:
             btnl     = i
             xx       = 100 * ii
             a        = wx.Button(self, 10, btnl, (xx,20))
# 3 Buttons are added and placed aside of each other
             a.myname = i
             self.Bind(wx.EVT_BUTTON, self.OnClick, a)
             print      i
             ii       = ii + 1

     #When the button is clicked
     def OnClick(self, event):
         global p , gs
         name = event.GetEventObject().myname
         p.SetSizer(gs)
         print "onclick is active " + str(name)

app = wx.App()
Example(None, title = 'Grid demo on-clicker')
app.MainLoop()


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: this works... 3 buttons with on-click bound

Igor Korot
Hi,

On Wed, Jun 14, 2017 at 11:26 AM,  <[hidden email]> wrote:

> import wx
>
> global p , gs
>
> class Example(wx.Frame):
>     def __init__(self, parent, title):
>         super(Example, self).__init__(parent, title = title,size =
> (500,300))
>         self.InitUI()
>         self.Centre()
>         self.Show()
>
>     def InitUI(self):
>         global p , gs
>         p  = wx.Panel(self)
>         gs = wx.GridSizer(11, 18, 5, 1)
>         A  = [ "He1", "He2 ","Ar3 " ]  # put more in if u want
>         ii = 1
>         for i in A:
>             btnl     = i
>             xx       = 100 * ii
>             a        = wx.Button(self, 10, btnl, (xx,20))
> # 3 Buttons are added and placed aside of each other
>             a.myname = i
>             self.Bind(wx.EVT_BUTTON, self.OnClick, a)
>             print      i
>             ii       = ii + 1
>
>     #When the button is clicked
>     def OnClick(self, event):
>         global p , gs
>         name = event.GetEventObject().myname
>         p.SetSizer(gs)
>         print "onclick is active " + str(name)
>
> app = wx.App()
> Example(None, title = 'Grid demo on-clicker')
> app.MainLoop()

You do realize that this code is completely unportable?
And you have a sizer that is never used, but instead you hardcoded
position/size of the controls.

Please check wxPython demo on how to work with sizers and change
your code accordingly.

Thank you.

>
>
> --
> You received this message because you are subscribed to the Google Groups
> "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

how to work with sizers

firefox

> Please check wxPython demo on how to work with sizers and change
> your code accordingly.



I only later found out that the OP had already fixed everything in his
github, incl. sizers and what not...


;-)



--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: this works... 3 buttons with on-click bound

Matt Newville
In reply to this post by firefox


On Wed, Jun 14, 2017 at 11:26 AM, <[hidden email]> wrote:
import wx

global p , gs

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title = title,size = (500,300))
        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):
        global p , gs
        p  = wx.Panel(self)
        gs = wx.GridSizer(11, 18, 5, 1)
        A  = [ "He1", "He2 ","Ar3 " ]  # put more in if u want
        ii = 1
        for i in A:
            btnl     = i
            xx       = 100 * ii
            a        = wx.Button(self, 10, btnl, (xx,20))
# 3 Buttons are added and placed aside of each other
            a.myname = i
            self.Bind(wx.EVT_BUTTON, self.OnClick, a)
            print      i
            ii       = ii + 1

    #When the button is clicked
    def OnClick(self, event):
        global p , gs
        name = event.GetEventObject().myname
        p.SetSizer(gs)
        print "onclick is active " + str(name)

app = wx.App()
Example(None, title = 'Grid demo on-clicker')
app.MainLoop()


It seems like you're having trouble with building an interactive Periodic Table of Elements that responds to events.  If you're interested, an example of such a thing is at:

     https://github.com/xraypy/xraylarch/blob/master/plugins/wx/periodictable.py
 
It may not be perfect (suggestions welcome) but it's self contained and works well for me as a panel component embedded in a couple of applications where I overload the "onselect" action.  It may give you some ideas for how this sort of thing can be implemented.

Cheers,

--Matt Newville

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.