Reputation: 453
I have a simple application based on tkinter and ttk. I have a notebook widget supposed to create a limited number of tabs and the tabs are the same thing. But I need to do different actions on each one. When I press some button a tab with its own name is created and the event binding will be focus on it. If I select the previous tab that was created with button press, the event binding will not focus on it nor its children widgets and this is the problem I need to solve. Can I toggle event binding between tabs? Any suggestions? I am using python 2.7
Upvotes: 3
Views: 3936
Reputation: 142681
See my answer to question how to make instances of event for every single tab on multi tab GUI tkinter( notebook widget) to see working example.
I use class MyTab
to create new tab with own events binding - so I can create many identical tabs and every tab use own events binding. In example tabs show different message when you change tab.
You didn't attache code in your question so I can't add more detailed answer.
EDIT:
example from previous link + binding to frame:
MyTab
: self.bind("<Button-1>", self.clickFrame)
(left mouse call function in MyTab
Application
: tab.bind("<Button-3>", self.clickTab)
(right mouse call function in Application
code:
#!/usr/bin/env python
from Tkinter import *
import tkMessageBox
import ttk
#---------------------------------------------------------------------
class MyTab(Frame):
def __init__(self, root, name):
Frame.__init__(self, root)
self.root = root
self.name = name
self.entry = Entry(self)
self.entry.pack(side=TOP)
self.entry.bind('<FocusOut>', self.alert)
self.entry.bind('<Key>', self.printing)
self.bind("<Button-1>", self.clickFrame)
#-------------------------------
def alert(self, event):
print 'FocusOut event is working for ' + self.name + ' value: ' + self.entry.get()
#tkMessageBox.showinfo('alert', 'FocusOut event is working for ' + self.name + ' value: ' + self.entry.get())
#-------------------------------
def printing(self, event):
print event.keysym + ' for ' + self.name
#-------------------------------
def clickFrame(self, event):
print "MyTab: click at (" + str(event.x) + ", " + str(event.y) + ') for ' + self.name + " (parent name: " + self.root.tab(CURRENT)['text'] + ")"
#---------------------------------------------------------------------
class Application():
def __init__(self):
self.tabs = {'ky':1}
self.root = Tk()
self.root.minsize(300, 300)
self.root.geometry("1000x700")
self.notebook = ttk.Notebook(self.root, width=1000, height=650)
# self.all_tabs = []
self.addTab('tab1')
self.button = Button(self.root, text='generate', command=self.start_generating).pack(side=BOTTOM)
self.notebook.pack(side=TOP)
#-------------------------------
def addTab(self, name):
tab = MyTab(self.notebook, name)
tab.bind("<Button-3>", self.clickTab)
self.notebook.add(tab, text="X-"+name)
# self.all_tabs.append(tab)
#-------------------------------
def clickTab(self, event):
print "Application: click at (" + str(event.x) + ", " + str(event.y) + ') for ' + event.widget.name
#-------------------------------
def start_generating(self):
if self.tabs['ky'] < 4:
self.tabs['ky'] += 1
self.addTab('tab'+ str(self.tabs['ky']))
#-------------------------------
def run(self):
self.root.mainloop()
#----------------------------------------------------------------------
Application().run()
Upvotes: 4