Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

python - how to make instances of event for every single tab on multi tab GUI tkinter( notebook widget)

this is my code.. and i need the event binding to work for all tabs the same time..the problem is when generate new tab the event binding will be related to the last generated tab and not working any more for pre-generated tabs..please help--(python 2.7)

#!/usr/bin/env python
import Tkinter
from Tkinter import *
import tkMessageBox
import ttk



root = Tkinter.Tk()
root.minsize(300,300)
root.geometry("1000x700")

body=ttk.Notebook(root,width=1000,height=650,)

tabs={'ky':1} 
def generate_tabs(tab,name):

  tab=Tkinter.Frame(body)                            
  entry=Entry(tab).pack(side=TOP)

  def alert(event):
    tkMessageBox.showinfo('alert1','FocusOut event is working for '+name)
  def printing(event):
    print event.keysym+' for '+name


  root.bind("<Key>",printing)

  tab.bind_class('Entry',"<FocusOut>",alert)
  body.add(tab,text=name)

def start_generating():
  if tabs['ky']==4:pass
  elif tabs['ky']==3: generate_tabs('tab4','tab4') ; tabs['ky']=4
  elif tabs['ky']==2: generate_tabs('tab3','tab3') ; tabs['ky']=3
  elif tabs['ky']==1: generate_tabs('tab2','tab2') ; tabs['ky']=2
  else:pass
generate_tabs('tab1','tab1')
button=Button(root,text='generate',command=start_generating).pack(side=LEFT)
body.pack(side=TOP)
root.mainloop()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I use class MyTab to create new tab with own function alert (called when you focus out entry) and printing (called when you type in entry).

I use print in place of tkMessageBox because messagebox focuse out entry on new selected tab (when you change tab) so you don't see messagebox from previous tab, but from current tab.

#!/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)

    #-------------------------------

    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

#---------------------------------------------------------------------

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)
        self.notebook.add(tab, text=name)
#       self.all_tabs.append(tab)

    #-------------------------------

    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()     

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...