Passing keywords to methods in tkinter

Stephen Boulet stephen.boulet at motorola.com
Tue Jan 21 16:03:24 EST 2003


Laura helped me with this one. The solution was to pass in the dictionary with 
its name preceded by two asterisks:

from Tkinter import *

class MyButton(Button):
	def __init__(self,parent,**kw):
		self.txt=kw['text']
		kw['command']=self.getValue
		apply(Button.__init__, (self,parent),kw)
		
	def getValue(self):
		print self.txt

class App:
	def __init__(self,parent):
		for i in range(1,4):
			buttonDict = {'text':i, 'fg':'red'}
			################## here ##########################
			MyButton(parent,**buttonDict).grid(row=1,column=i-1)

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


Stephen Boulet wrote:
> I'm having trouble passing arbitrary keywords to tkinter classes.
> 
> When I call the class MyButton's init method from the App class, I get 
> the error that the init method takes exactly 3 arguments, but 4 are 
> provided.
> 
> Thanks for any help.
> 
> -- Stephen
> 
> 
> ########################################################
> from Tkinter import *
> 
> class MyButton(Button):
>     def __init__(self,parent,data,**kw):
>         self.txt=data
>         apply(Button.__init__, (self,parent),kw)
>        
>     def getValue(self):
>         print self.txt
> 
> class App:
>     def __init__(self,parent):
>         buttonDict = {'text':'some text', 'fg':'red'}
> 
>         # problem is here
>         MyButton(parent,'1',buttonDict).grid(row=1,column=0)
>         MyButton(parent,'2',buttonDict).grid(row=1,column=1)
>         MyButton(parent,'3',buttonDict).grid(row=1,column=2)
> 
> if __name__=='__main__':
>     root=Tk()
>     app=App(root)
>     root.mainloop()
> 






More information about the Python-list mailing list