473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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

May 12 '07 #1
3 2136
On May 12, 11:04 am, Thomas Jansson <tjansso...@gmail.comwrote:
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 '07 #2
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
May 13 '07 #3
On 13 Maj, 08:45, Peter Otten <__pete...@web.dewrote:
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

May 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
1
by: Elaine Jackson | last post by:
My CheckButton variables seem always to be true, whether the box is checked or not, which makes them considerably less useful. Following is a little script that illustrates the difficulty. Any help...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
3
by: Tuvas | last post by:
I want to have a checkbutton that when it is pushed will do a function depending on if it was pushed before or not. Ei: b=checkbutton(command=check) b.grid(row=0,column=0) def check(): if...
4
by: Ronnie | last post by:
Ok let me just say first that I am a newbie in Access and I don't know much of SQL or VB programming. But I am trying to create this contact database using Access 97. I have created 2 tables,...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
1
by: skyson2ye | last post by:
Hi, guys: I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem,...
3
by: jayro | last post by:
Hi folks, I got a checkbutton with a large label which is being wrapped into a few lines. Due to the height of the label, the checkbutton aligns vertically ending up set on the center of its...
0
by: dudeja.rajat | last post by:
Hi, I've a checkbutton in my GUI application which I want to work as: 1. it should be un-ticked by default, 2. should display a label in Gui, by default, 3. when user ticks the check button...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.