I am making a GUI of the periodic table of elements with wxpython, I have added all the buttons. So now what I want is when the user selects multiple buttons it should give me output same as the labeled on the button. Trying to run this code but getting the error. Don't know what's wrong, I am doing here.
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)
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()
Well, it seems that the third argument in
self.Bind(wx.EVT_BUTTON, self.OnClick, a)
is useless, tryself.Bind(wx.EVT_BUTTON, self.OnClick)
(comparing with code generated thanks to wxformbuilder)
It's all a bit tight given the number of buttons and you must declare the buttons as part of the panel, not hope that the sizer will do the work for you.
There are few changes but they are important.
Code now includes elements{dictionary} for element display (I'm missing a few of the rarer elements like Oganesson but hey if you can't eat it, smoke it, make a purchase with it or use it to scale a mountain, who cares?)
New version based on your other question Error with on click event in wxpython
This is now the third time that I have asked you to read how StackOverflow works, please read the tour pages. Ultimately, this is for your own good.