[Tutor] Adding attributes to imported class
Michael Lange
klappnase at freenet.de
Wed Jul 6 21:05:09 CEST 2005
Hello list,
I'm trying to write a python wrapper for the tkDnD Tk extension to add drag and drop support
to Tkinter. Looking for a way to give all widgets access to the newly defined dnd methods
I came to create a subclass of Tkinter.Tk() and pass the dnd methods to Tkinter.BaseWidget()
(all Tkinter widgets except Tk() inherit from BaseWidget()), like this:
###### file TkinterDnD.py #########################
import Tkinter
class Tk(Tkinter.Tk):
def __init__(self, *args, **kw):
Tkinter.Tk.__init__(self, *args, **kw)
self.tk.eval('package require tkdnd')
def dnd_clearsource(self):
'''Unregister widget as drag source.'''
return self.tk.call('dnd', 'clearsource', self)
Tkinter.BaseWidget.dnd_clearsource = dnd_clearsource
< etc. >
###################################################
This is nice, because now I can simply do:
from Tkinter import *
import TkinterDnD
root = TkinterDnD.Tk()
to magically add dnd methods to all Tkinter widgets.
However, I can't help that this trick somehow reminds me of a do-it-yourself lobotomy,
so now my question, is this a common way to add new attributes to existing classes
or is it possible that this causes problems that I don't see at the moment?
Thanks in advance
Michael
More information about the Tutor
mailing list