473,839 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.checkbutto n = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
"LCOMP"]
for item in self.checkbutto n:
self.+'item'+Ch eckbutton = Chekcbutton(fra me, onvalue='t',
offvalue='f', variable=self.+ 'item'+)
self.+'item'+Ch eckbutton.grid( )

How should I do this?

Kind regards
Thomas Jansson

May 12 '07 #1
3 2158
On May 12, 11:04 am, Thomas Jansson <tjansso...@gma il.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.checkbutto n = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
"LCOMP"]
for item in self.checkbutto n:
self.+'item'+Ch eckbutton = Chekcbutton(fra me, onvalue='t',
offvalue='f', variable=self.+ 'item'+)
self.+'item'+Ch eckbutton.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=Non e)
self.names = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR", "LCOMP",
"Sean"]
self.checkbutto ns = []

self.f = Frame(root)
for name in self.names:
self.checkbutto ns.append(CButt on(parent=self. f, name=name,
default="f"))

self.f.pack(sid e="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.s et(default)
self.checkbutto n = None
self.create_che ckbox(name)

def create_checkbox (self,name):
f = Frame(self.pare nt)
Label(f, text=name).pack (side="left")
self.checkbutto n = Checkbutton(f, onvalue='t', offvalue='f',
variable=self.v ariable)
self.checkbutto n.bind("<Button-1>", self.state_chan ged)
self.pack()
f.pack()

def pack(self):
self.checkbutto n.pack()

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

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

~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.checkbutto n = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
"LCOMP"]
for item in self.checkbutto n:
self.+'item'+Ch eckbutton = Chekcbutton(fra me, onvalue='t',
offvalue='f', variable=self.+ 'item'+)
self.+'item'+Ch eckbutton.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_checkbut tons(self):
button_names = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR", "LCOMP"]
self.cbvalues = {}
for row, name in enumerate(butto n_names):
v = self.cbvalues[name] = IntVar()
cb = Checkbutton(sel f.frame, variable=v)
label = Label(self.fram e, 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.checkbutto n = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR",
"LCOMP"]
for item in self.checkbutto n:
self.+'item'+Ch eckbutton = Chekcbutton(fra me, onvalue='t',
offvalue='f', variable=self.+ 'item'+)
self.+'item'+Ch eckbutton.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_checkbut tons(self):
button_names = ["LNCOL", "LFORM", "LPOT", "LGRID", "LERR", "LCOMP"]
self.cbvalues = {}
for row, name in enumerate(butto n_names):
v = self.cbvalues[name] = IntVar()
cb = Checkbutton(sel f.frame, variable=v)
label = Label(self.fram e, 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_nam es = ["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(fra me, onvalue="t", offvalue="f", variable=v)
label = Label(frame, text=name)
label.grid(row= row+15, column=0, sticky=W)
self.cb.grid(ro w=row+15, column=1, sticky=W)
if button_state[row] == "t":
self.cb.select( )
else:
self.cb.deselec t()

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
4969
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
1
3260
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 will be greatly appreciated. Thank you. ########################################### from Tkinter import * def bCommand():
7
6304
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 passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
3
7663
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 (b.value==0): do_stuff_here() elif(b.value==1)
4
2032
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, one Personal (it has all the personal information) and the other one Organization (this one has details of diff organizations). One person can belong to more than any (upto 5 in this case) organizations. So I included orgid1, orgid2, orgid3, orgid4...
17
46593
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: The goal of this tutorial is to design a Data Abstraction Layer (DAL) in PHP, that will allow us to ignore the intricacies of MySQL and focus our attention on our Application Layer and Business Logic. Hopefully, by the end of this guide, you will...
1
3757
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, and I have spent three days trying to debug but to no avail. Everything is OK when there are only two dependent list boxes, but when adding the third child list box, a problem appears: if I populate the third box only with the value: new...
3
2639
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 space. Is there a way I can set the vertical alignment of the checkbutton so it remains in the top of its space if its label is too big (has many lines)? Thanks in advance,
0
857
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 this should the above label goes off the screen and not longer is displayed.
0
9854
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10584
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10290
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7015
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3131
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.