Re: 'connect' widget classes in an application

ingo <[email protected]>
Newsgroups gmane.comp.python.tkinter
Message-ID <[email protected]>
Op 2016-01-24 om 22:29 schreef Michael Lange:
>
> http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/events.html  (ff.)

Got the basics working now, took some time to figure out the dot in
  tab_id = "." + self.ed._name


Thanks again,

Ingo


import tkinter as tk
from tkinter import ttk

class Ttoolbar(ttk.Frame):

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

      def add_button(self, **kw):
           b = ttk.Button(self, **kw)
           b.pack(side=tk.LEFT)
           self._tb_items.append(b)


class Eedit(tk.Text):

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


class Nnotebook(ttk.Notebook):

      def __init__(self, parent=None):
           ttk.Notebook.__init__(self, parent)
           self.pack(fill=tk.BOTH, expand=tk.Y)
           self.config(width=400, height=400)
           self.bind_class("TNotebook", "<Double-ButtonPress-1>", 
self.add_edtab)
           self.bind_all("<Control-n>", self.add_edtab)
           self.tab_ref={}

      def add_edtab(self, event=None, text=None):
           self.ed = Eedit()
           self.add(self.ed, text="untitled")
           tab_id = "." + self.ed._name
           self.tab_ref[tab_id] = self.ed
           self.select(tab_id)

      def active_tab(self):
           return self.tab_ref[self.select()]


class Aapp(ttk.Frame):

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

           self.tb = Ttoolbar()
           self.tb.pack(side=tk.TOP, fill=tk.X)
           self.nb = Nnotebook()
           self.nb.pack()

           self.tb.add_button(
               text='Add Tab',
               command=lambda: self.nb.add_edtab()
           )
           self.tb.add_button(
               text='Add Text to Active Tab',
               command=lambda: self.nb.active_tab().insert(tk.END, 
'Button pressed.\n')
           )


if __name__ == '__main__':
      app = Aapp()
      app.pack()
      app.mainloop()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.