Создаёт независимые окна-фреймы с произвольным содержанием в ввиде вкладок на основе Frame и Radiobutton. Пример использования после скрипта.
1 # file: notebook.py
2 # A simple notebook-like Tkinter widget.
3 # Copyright 2003, Iuri Wickert (iwickert yahoo.com)
4
5 from Tkinter import *
6
7 class notebook:
8
9
10 # initialization. receives the master widget
11 # reference and the notebook orientation
12 def __init__(self, master, side=LEFT):
13
14 self.active_fr = None
15 self.count = 0
16 self.choice = IntVar(0)
17
18 # allows the TOP and BOTTOM
19 # radiobuttons' positioning.
20 if side in (TOP, BOTTOM):
21 self.side = LEFT
22 else:
23 self.side = TOP
24
25 # creates notebook's frames structure
26 self.rb_fr = Frame(master, borderwidth=2, relief=RIDGE)
27 self.rb_fr.pack(side=side, fill=BOTH)
28 self.screen_fr = Frame(master, borderwidth=2, relief=RIDGE)
29 self.screen_fr.pack(fill=BOTH)
30
31
32 # return a master frame reference for the external frames (screens)
33 def __call__(self):
34
35 return self.screen_fr
36
37
38 # add a new frame (screen) to the (bottom/left of the) notebook
39 def add_screen(self, fr, title):
40
41 b = Radiobutton(self.rb_fr, text=title, indicatoron=0, \
42 variable=self.choice, value=self.count, \
43 command=lambda: self.display(fr))
44 b.pack(fill=BOTH, side=self.side)
45
46 # ensures the first frame will be
47 # the first selected/enabled
48 if not self.active_fr:
49 fr.pack(fill=BOTH, expand=1)
50 self.active_fr = fr
51
52 self.count += 1
53
54 # returns a reference to the newly created
55 # radiobutton (allowing its configuration/destruction)
56 return b
57
58
59 # hides the former active frame and shows
60 # another one, keeping its reference
61 def display(self, fr):
62
63 self.active_fr.forget()
64 fr.pack(fill=BOTH, expand=1)
65 self.active_fr = fr
66
67 # END
Пример:
1 # ###-------------------------------
2 # # file: test.py
3 # # simple demonstration of the Tkinter notebook
4
5 # from Tkinter import *
6 # from notebook import *
7
8 a = Tk()
9 n = notebook(a, LEFT)
10
11 # uses the notebook's frame
12 f1 = Frame(n())
13 b1 = Button(f1, text="Button 1")
14 e1 = Entry(f1)
15 # pack your widgets before adding the frame
16 # to the notebook (but not the frame itself)!
17 b1.pack(fill=BOTH, expand=1)
18 e1.pack(fill=BOTH, expand=1)
19
20 f2 = Frame(n())
21 # this button destroys the 1st screen radiobutton
22 b2 = Button(f2, text='Button 2', command=lambda:x1.destroy())
23 b3 = Button(f2, text='Beep 2', command=lambda:Tk.bell(a))
24 b2.pack(fill=BOTH, expand=1)
25 b3.pack(fill=BOTH, expand=1)
26
27 f3 = Frame(n())
28
29 # keeps the reference to the radiobutton (optional)
30 x1 = n.add_screen(f1, "Screen 1")
31 n.add_screen(f2, "Screen 2")
32 n.add_screen(f3, "dummy")
33
34 if __name__ == "__main__":
35 a.mainloop()