Wrap Tk widget using a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Walzer

    Wrap Tk widget using a class

    I'm trying to wrap a subset of a Tcl/Tk widget set called tclmacbag (see
    http://tclmacbag.autons.net/) for use in my Tkinter application, using a
    "macnoteboo k" class. I'm having some difficulty getting things
    configured correctly.

    Here is my class code:

    from Tkinter import *

    class Macnotebook:

    def __init__(self, master):

    self.master = master
    self.master.cal l('package', 'require', 'tclmacbag')


    def notebook(self):

    self.master.cal l('::tclmacbag: :pnb', self)


    def add(self, child):
    self.master.cal l('::tclmacbag: :pnb', 'add', child)

    Here is an example of how I'm calling this in my code:

    from Macnotebook import Macnotebook

    self.prefbook = Macnotebook.not ebook(self.pref frame)
    self.prefbook.p ack(fill=BOTH, expand=YES, side=TOP)

    This returns the following error in my console:

    Traceback (most recent call last):

    self.prefbook = Macnotebook.not ebook(self.pref frame)
    TypeError: unbound method notebook() must be called with Macnotebook
    instance as first argument (got Frame instance instead)

    Can anyone suggest how I might better structure the class so that this
    works? I'm a bit of a newbie with OO, so any pointers are appreciated.

    --
    Kevin Walzer
    Code by Kevin

  • Fredrik Lundh

    #2
    Re: Wrap Tk widget using a class

    Kevin Walzer wrote:

    Here is an example of how I'm calling this in my code:
    >
    from Macnotebook import Macnotebook
    >
    self.prefbook = Macnotebook.not ebook(self.pref frame)
    self.prefbook.p ack(fill=BOTH, expand=YES, side=TOP)
    you're attempting to call the method in a class object. I suspect that
    you have to create an instance of that class first:

    self.prefbook = Macnotebook(sel f.prefframe)
    self.prefbook.n otebook() # create it
    self.prefbook.p ack(...)

    # call self.prefbook.a dd() to add pages to the notebook

    (it's probably a good idea to move the notebook creation code into the
    __init__ method; two-stage construction isn't very pythonic...)

    </F>

    Comment

    Working...