[Tkinter-discuss] 'connect' widget classes in an application
ingo
ingoogni at gmail.com
Sun Jan 24 06:58:05 EST 2016
Extended the code with a ttk.notebook and a button. Seems to work. Text
is added only at the last opened tab, by the button, I think I
understand why but will look into that later.
When I add a tab through the button, no event is generated. When adding
through a double-click or ctrl-n it does. Where does this difference
come from?
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)
def add_edtab(self, event=None):
if event:
print(event)
self.ed = Eedit()
self.add(self.ed, text="test")
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',
command=lambda: self.nb.ed.insert(tk.END, 'Button
pressed.\n')
)
if __name__ == '__main__':
app = Aapp()
app.pack()
app.mainloop()
More information about the Tkinter-discuss
mailing list