[Tutor] Recursive Tkinter buttons

Adam Cripps kabads at gmail.com
Fri Feb 25 07:53:56 CET 2005


On Thu, 24 Feb 2005 21:26:29 -0500, Kent Johnson <kent37 at tds.net> wrote:
> Adam Cripps wrote:
> > I'm trying to create recursive Tkinter buttons with:
> >
> >               for i in range(0,10):
> >                       print i
> >                       buttonlabel = "field " +str(i)
> >                       button[i] = Button (text=buttonlabel)
> >                       button[i].grid(column=3, row = i+3)
> >
> > However, Tkinter doesn't seem to like it with
> 
> Please show us the whole program. This works for me:
> 
> from Tkinter import *
> 
> root = Tk()
> button = {}
> for i in range(0,10):
>      print i
>      buttonlabel = "field " +str(i)
>      button[i] = Button (text=buttonlabel)
>      button[i].grid(column=3, row = i+3)
> 
> root.mainloop()
> 
> Kent

Sorry guys for not be clear - recursive isn't the correct term  -
that's what you get when you fire off an email late at night.

I want to create 10 buttons through a loop, each with different text
and passing a different var to another function. My current code is:
(the bit I'm currently working on is near the bottom)

#!/usr/bin/env python
from Tkinter import *
from tkFont import *
from string import *
from sys import* 
from re import *

class Application(Frame):
	def __init__(self, master=None, geometry="500x200"):
		"""Starts the application off"""
		Frame.__init__(self,master)
		self.grid()
		self.addMenu()
		self.createWidgets()

	def quit(self):
		self.master.quit()
	

	def save(self):
		print "saving..."

	def open(self):
		print "opening..."
	
	def about(self):
		print "about"

	
	def addMenu(self):
		menu = Menu(root)
		root.config(menu=menu)

		filemenu = Menu(menu, tearoff=0)
		menu.add_cascade(label="File", menu=filemenu)
		filemenu.add_command(label="Open", command=self.open)
		filemenu.add_command(label="Save...", command=self.save)
		filemenu.add_separator()
		filemenu.add_command(label="Exit", command=self.quit)

		helpmenu = Menu(menu, tearoff=0)
		menu.add_cascade(label="Help", menu=helpmenu)
		helpmenu.add_command(label="About...", command=self.about)

	def createWidgets(self):
		"""This creates all the widgets in the main frame"""
		titleLabel = Label(text="Squawk report generator")
		titleLabel.grid(column = 0, row=0)
		firstNameLabel = Label(text="First name: ")
		firstNameLabel.grid(column = 0, row  = 3, sticky = NW)
		self.firstname = StringVar()
		firstNameEntry = Entry(textvariable = self.firstname)
		firstNameEntry.grid(column =1, row=3, sticky = NW)
		surNameLabel = Label(text="Surname: ")
		surNameLabel.grid(column = 0, row = 4, sticky = NW)
		self.surname= StringVar()
		surNameEntry = Entry(textvariable=self.surname)
		surNameEntry.grid(column = 1, row=4, stick=NW)
		button = Button(text ="hello")
		
		#Now we will loop around showing the entry fields
		button = []
		for i in range(0,10):
			print i
			buttonlabel = "field " +str(i)
			button[i].append = Button (text=buttonlabel)
			button[i].grid(column=3, row = i+3)

root = Tk()
app = Application(root)
app.master.title("Squawk Report Generator")
app.mainloop()

The current error is:

  File "report.py", line 80, in createWidgets
    button[i].append = Button (text=buttonlabel)
IndexError: list index out of range

TIA
Adam
-- 
http://www.monkeez.org
PGP key: 0x7111B833


More information about the Tutor mailing list