Creating a function to make checkbutton with information from a list?

Discussion in 'Python' started by Thomas Jansson, May 12, 2007.

  1. Dear all

    I am writing a program with tkinter where I have to create a lot of
    checkbuttons. They should have the same format but should have
    different names. My intention is to run the functions and the create
    all the buttons with the names from the list.

    I now the lines below doesn't work, but this is what I have so far. I
    don't really know how call the element in the dict use in the for
    loop. I tried to call +'item'+ but this doesn't work.

    def create_checkbox(self):
    self.checkbutton = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
    "LCOMP"]
    for item in self.checkbutton:
    self.+'item'+Checkbutton = Chekcbutton(frame, onvalue='t',
    offvalue='f', variable=self.+'item'+)
    self.+'item'+Checkbutton.grid()

    How should I do this?

    Kind regards
    Thomas Jansson
    Thomas Jansson, May 12, 2007
    #1
    1. Advertising

  2. Thomas Jansson

    Guest

    On May 12, 11:04 am, Thomas Jansson <> wrote:
    > Dear all
    >
    > I am writing a program with tkinter where I have to create a lot of
    > checkbuttons. They should have the same format but should have
    > different names. My intention is to run the functions and the create
    > all the buttons with the names from the list.
    >
    > I now the lines below doesn't work, but this is what I have so far. I
    > don't really know how call the element in the dict use in the for
    > loop. I tried to call +'item'+ but this doesn't work.
    >
    > def create_checkbox(self):
    > self.checkbutton = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
    > "LCOMP"]
    > for item in self.checkbutton:
    > self.+'item'+Checkbutton = Chekcbutton(frame, onvalue='t',
    > offvalue='f', variable=self.+'item'+)
    > self.+'item'+Checkbutton.grid()
    >
    > How should I do this?
    >
    > Kind regards
    > Thomas Jansson


    You can use exec("self." + name + " = " + value) to do what you want,
    but then you need to exec() each time you want to access the
    variable. I think it is much better to create a class.

    Here's what I came up with:

    from Tkinter import *

    class Window(Frame):
    def __init__(self, parent=None):
    Frame.__init__(self,parent=None)
    self.names = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR", "LCOMP",
    "Sean"]
    self.checkbuttons = []

    self.f = Frame(root)
    for name in self.names:
    self.checkbuttons.append(CButton(parent=self.f, name=name,
    default="f"))

    self.f.pack(side="top",padx=5, pady=5)


    class CButton(object):
    def __init__(self, parent=None, name=None, default=None):
    self.name = name
    self.parent = parent
    self.variable = StringVar()
    self.variable.set(default)
    self.checkbutton = None
    self.create_checkbox(name)

    def create_checkbox(self,name):
    f = Frame(self.parent)
    Label(f, text=name).pack(side="left")
    self.checkbutton = Checkbutton(f, onvalue='t', offvalue='f',
    variable=self.variable)
    self.checkbutton.bind("<Button-1>", self.state_changed)
    self.pack()
    f.pack()

    def pack(self):
    self.checkbutton.pack()

    def state_changed(self, event=None):
    print "%s: %s" % (self.name, self.variable.get())

    if __name__ == '__main__':
    root = Tk()
    Window().mainloop()

    ~Sean
    , May 12, 2007
    #2
    1. Advertising

  3. Thomas Jansson

    Peter Otten Guest

    Thomas Jansson wrote:

    > Dear all
    >
    > I am writing a program with tkinter where I have to create a lot of
    > checkbuttons. They should have the same format but should have
    > different names. My intention is to run the functions and the create
    > all the buttons with the names from the list.
    >
    > I now the lines below doesn't work, but this is what I have so far. I
    > don't really know how call the element in the dict use in the for
    > loop. I tried to call +'item'+ but this doesn't work.
    >
    > def create_checkbox(self):
    > self.checkbutton = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
    > "LCOMP"]
    > for item in self.checkbutton:
    > self.+'item'+Checkbutton = Chekcbutton(frame, onvalue='t',
    > offvalue='f', variable=self.+'item'+)
    > self.+'item'+Checkbutton.grid()
    >
    > How should I do this?


    You /could/ use setattr()/getattr(), but for a clean design putting the
    buttons (or associated variables) into a dictionary is preferrable.

    def create_checkbuttons(self):
    button_names = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR", "LCOMP"]
    self.cbvalues = {}
    for row, name in enumerate(button_names):
    v = self.cbvalues[name] = IntVar()
    cb = Checkbutton(self.frame, variable=v)
    label = Label(self.frame, text=name)
    cb.grid(row=row, column=0)
    label.grid(row=row, column=1)

    You can then find out a checkbutton's state with

    self.cbvalues[name].get()

    Peter
    Peter Otten, May 13, 2007
    #3
  4. On 13 Maj, 08:45, Peter Otten <> wrote:
    > Thomas Jansson wrote:
    > > Dear all

    >
    > > I am writing a program with tkinter where I have to create a lot of
    > > checkbuttons. They should have the same format but should have
    > > different names. My intention is to run the functions and the create
    > > all the buttons with the names from the list.

    >
    > > I now the lines below doesn't work, but this is what I have so far. I
    > > don't really know how call the element in the dict use in the for
    > > loop. I tried to call +'item'+ but this doesn't work.

    >
    > > def create_checkbox(self):
    > > self.checkbutton = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
    > > "LCOMP"]
    > > for item in self.checkbutton:
    > > self.+'item'+Checkbutton = Chekcbutton(frame, onvalue='t',
    > > offvalue='f', variable=self.+'item'+)
    > > self.+'item'+Checkbutton.grid()

    >
    > > How should I do this?

    >
    > You /could/ use setattr()/getattr(), but for a clean design putting the
    > buttons (or associated variables) into a dictionary is preferrable.
    >
    > def create_checkbuttons(self):
    > button_names = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR", "LCOMP"]
    > self.cbvalues = {}
    > for row, name in enumerate(button_names):
    > v = self.cbvalues[name] = IntVar()
    > cb = Checkbutton(self.frame, variable=v)
    > label = Label(self.frame, text=name)
    > cb.grid(row=row, column=0)
    > label.grid(row=row, column=1)
    >
    > You can then find out a checkbutton's state with
    >
    > self.cbvalues[name].get()
    >
    > Peter


    Both of you for your answers I ended up using the last one since it
    seemed least complicated to new python programmer as my self. In the
    case that anyone should ever read the post again and would like to see
    what I ended up with:

    self.button_names = ["LPOT", "LNCOL", "LFORM", "LGRID", "LERR",
    "LCOMP", "LMAP", "LPUNCH", "LMEAN"]
    button_state = ["t" , "t" , "t" , "t" , "f" ,
    "f" , "f" , "t" , "f" ]
    self.cbvalues = {}
    for row, name in enumerate(self.button_names):
    v = self.cbvalues[name] = StringVar() # It is a string variable so,
    t or f can be store here
    self.cb = Checkbutton(frame, onvalue="t", offvalue="f", variable=v)
    label = Label(frame, text=name)
    label.grid(row=row+15, column=0, sticky=W)
    self.cb.grid(row=row+15, column=1, sticky=W)
    if button_state[row] == "t":
    self.cb.select()
    else:
    self.cb.deselect()

    Kind regards
    Thomas Jansson
    Thomas Jansson, May 14, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Askari
    Replies:
    2
    Views:
    381
    Eric Brunel
    Jan 15, 2004
  2. Elaine Jackson

    Tkinter CheckButton variables

    Elaine Jackson, Jul 1, 2004, in forum: Python
    Replies:
    1
    Views:
    422
    klappnase
    Jul 1, 2004
  3. Paul Rubin
    Replies:
    3
    Views:
    731
    Eric Brunel
    Jul 4, 2005
  4. Tuvas

    Tkinter- checkbutton

    Tuvas, Nov 4, 2005, in forum: Python
    Replies:
    3
    Views:
    1,090
    Tuvas
    Nov 4, 2005
  5. O.R.Senthil Kumaran
    Replies:
    3
    Views:
    952
    Eric Brunel
    Jul 5, 2007
Loading...

Share This Page