Hi,

On Sat, 23 Jan 2016 10:05:21 +0100
ingo <ingoo...@gmail.com> wrote:

> In the code below I get the error below.
> I'm trying to create a class for several widgets and make the
> "interact" within a main Frame. Can't figure it out (lack of
> programming experience) and couldn't find an eye opening example.
> 
> I understand that the toolbar has no way to access the txt in the 
> editor, how do I make it so?
> 

there are several problems with your example:

First (not critical though), you pack() the editor and toolbar widgets
twice, once in their respective __init__() methods and a second time in
Aapp.makewidgets(). This is not really an error, but does of course not
make much sense; in order to help you keep an overview of your widgets in
more complex layouts I would strongly recommend to skip the pack'ing in
the widget's __init__ and leave geometry management to the parent class.

Second, as you noticed yourself, in your approach there is no way to
access the Text widget from the toolbar. There are several possible ways
to fix this; for a toolbar class my suggestion is to add method(s) to the
class that allow to modify the toolbar from the outside.

Third, you do not keep any reference to the child widgets (toolbar and
editor) in the main Aapp class (a line like ed = Eedit().pack() simply
sets ed to None).

This modified example shows one possible way to handle these issues:

from tkinter import *

class Ttoolbar(Frame):

     def __init__(self, parent=None):
         Frame.__init__(self, parent)
         self._buttons = []

     def add(self, **kw):
         b = Button(self, **kw)
         b.pack(side=LEFT)
         self._buttons.append(b)


class Eedit(Text):

     def __init__(self, parent=None):
         txt = Text.__init__(self, parent)


class Aapp(Frame):

     def __init__(self, parent=None):
         Frame.__init__(self, parent)

         self.tb = Ttoolbar()
         self.tb.pack(side=TOP, fill=X)
         self.ed = Eedit()
         self.ed.pack()

         self.tb.add(text='Button',
             command=lambda: self.ed.insert(END, 'Button pressed.\n'))


if __name__ == '__main__':
     app = Aapp()
     app.pack()
     app.mainloop()

I hope this helps.

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Vulcans do not approve of violence.
                -- Spock, "Journey to Babel", stardate 3842.4
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to