1 # tk022
2 # switch between frames
3 #
4 # fredrik lundh, january 1998
5 #
6 # fredrik@pythonware.com
7 # http://www.pythonware.com
8 #
9
10 from Tkinter import *
11
12 root = Tk()
13
14 # each view is a frame, using a common frame ('view')
15 # as their parent
16
17 view = Frame(root)
18
19 f1 = Frame(view)
20
21 w = Label(f1, text="This is the first frame")
22 w.grid(row=1, column=1, columnspan=2)
23
24 w = Label(f1, text="Entry")
25 w.grid(row=2, column=1)
26
27 w = Entry(f1)
28 w.grid(row=2, column=2)
29
30 f2 = Frame(view)
31
32 w = Message(f2, text="This is the second frame, "
33 "which contains a message widget")
34 w.pack()
35
36 f3 = Frame(view)
37
38 w = Text(f3, width=20, height=10, wrap=WORD)
39 w.insert(END, "And here's the third frame, "
40 "which contains an editable text widget")
41 w.pack()
42
43 viewframes = [f1, f2, f3]
44
45 # display the views
46
47 # change to 'if 0:' to get variable size
48 # change to 'if 1:' to get fixed window size
49
50 if 0:
51
52 # stuff to display a given view in a window with variable size
53
54 def show(index):
55
56 # unmap existing views
57 for w in view.pack_slaves():
58 w.pack_forget()
59
60 viewframes[index].pack()
61
62 else:
63
64 # stuff to display a given view in a window with fixed size,
65 # like a standard notepad. note that this doesn't work if
66 # any of the subviews change their size after the following
67 # code has run
68
69 # determine the size of the largest view
70 x, y = 0, 0
71 for w in viewframes:
72 w.update_idletasks()
73 x = max(x, w.winfo_reqwidth())
74 y = max(y, w.winfo_reqheight())
75
76 view.config(width=x, height=y)
77
78 def show(index):
79
80 # unmap existing views
81 for w in view.place_slaves():
82 w.place_forget()
83
84 viewframes[index].place(relwidth=1, relheight=1)
85
86 # create a button bar
87
88 bar = Frame(root)
89
90 index = 0
91
92 var = IntVar()
93 var.set(0)
94
95 for text, command in (
96 ("One", lambda: show(0)),
97 ("Two", lambda: show(1)),
98 ("Three", lambda: show(2))
99 ):
100
101 b = Radiobutton(bar, text=text, indicatoron=0,
102 variable=var, value=index,
103 command=command)
104 b.pack(side=LEFT, padx=5)
105
106 index = index + 1
107
108 # pack the button bar and the view
109
110 bar.pack(pady=2)
111 view.pack()
112
113 # map the first view
114
115 show(var.get())
116
117 mainloop()
118
1 # tile canvas background (Fredrik Lundh, September 1997)
2
3 from Tkinter import *
4
5 root = Tk()
6
7 logo = PhotoImage(data="""
8 R0lGODlhNwAWAPIHAP///4SEhDk5OQBjpQAAAAAAAAAAAAAAACH+HUdpZkJ1aWxkZXIgMC41IGJ5
9 IFl2ZXMgUGlndWV0ACH5BAUKAAAALAAAAAA3ABYAAAP9CLrcPDDKCZ29eNHNaf7AIYzCIR7DSY5n
10 Wopse4LNIDDBXd5B0+8AQU+RC4qMlU+EJwT8jqNeTneUNqVQVAQjYUqdhyfV6zTyiNSJg3JbBMLl
11 d7btNsfLpY2G7YzB5XN9X0A/Q3l6ABtUJiiLNnaEgnhwhx4caUtIj0CcQ32QJpUdECdapC+njC+l
12 JKGqoZodMCwuMXmstjCqfiSxlzhwaJCeRUNvxMFPahM8AV85gyNxzmVOz9RShqYDCsw4Y3dfOGAw
13 PkbKW92ZU1VX4dZTRrDO8dCxa5uMwWDP+9KH/vYh4VIqwqxXq1oVPMVLIQhGEvShkJhCoomI+irq
14 oxOhbpTHjxwzfByZJKRJkulMAkgAADs=
15 """)
16
17 def background(canvas, bbox, image):
18 xsize, ysize = image.width(), image.height()
19 for x in range(bbox[0], bbox[2], xsize):
20 for y in range(bbox[1], bbox[3], ysize):
21 canvas.create_image(x, y, image=image) #anchor="nw" or "e" or ...
22
23 canvas = Canvas(root, width=500, height=500, bd=0, highlightthickness=0, bg="white")
24 canvas.pack()
25
26 background(canvas, (0, 0, 500, 500), logo)
27
28 root.mainloop()
29
1 # breaking out of event handlers (Fredrik Lundh, June 1997)
2 # compare the behaviour of keys "a" and "b" in the following example:
3 from Tkinter import *
4
5 root = Tk()
6
7 # event handlers
8 def argh(event):
9 text.insert(INSERT, "[argh!]")
10
11 def bleech(event):
12 text.insert(INSERT, "[bleech!]")
13 return "break"
14
15 # create a text widget
16 text = Text(root)
17 text.pack()
18
19 # add some event handlers on the widget instance level
20 text.bind("a", argh)
21 text.bind("b", bleech)
22
23 text.focus_set()
24
25 root.mainloop()
26
1 # tk029
2 # changing the canvas view
3 #
4 # fredrik lundh, march 1998
5 #
6 # fredrik@pythonware.com
7 # http://www.pythonware.com
8 #
9
10 from Tkinter import *
11
12 root = Tk()
13
14 s = Scrollbar(root, orient=HORIZONTAL)
15 c = Canvas(root, width=500, height=500,
16 scrollregion=(0, 0, 2000, 2000),
17 xscrollcommand=s.set)
18 s.config(command=c.xview)
19
20 for i in range(100, 2000+100, 100):
21 c.create_text(i, i, text=str(i))
22 c.create_text(i, 0, text=str(i), anchor=N)
23 c.create_text(0, i, text=str(i), anchor=W)
24
25 # pack the scrollbars first!
26 s.pack(side=BOTTOM, fill=X)
27 c.pack(fill=BOTH, expand=1)
28
29 # center the canvas view
30 width = c.winfo_reqwidth()
31 fullwidth = 2000
32
33 fraction = float(fullwidth - width) / fullwidth / 2
34
35 c.xview("moveto", fraction)
36
37 root.mainloop()
38
1 # tk041
2 # grab all events when focussed
3 #
4 # fredrik lundh, june 1998
5 #
6 # fredrik@pythonware.com
7 # http://www.pythonware.com
8 #
9
10 from Tkinter import *
11
12 class GrabbingEntry(Entry):
13
14 def __init__(self, master=None, **kw):
15 apply(Entry.__init__, (self, master), kw)
16
17 self.bind("<FocusIn>", self.__focus)
18 self.bind("<FocusOut>", self.__blur)
19 self.bind("<Escape>", self.__blur)
20
21 def __focus(self, event):
22 self.grab_set()
23
24 def __blur(self, event):
25 self.grab_release()
26
27 #
28 # test stuff
29
30 root = Tk()
31
32 w = Label(root, text="Spam")
33 w.grid(row=1, column=1)
34
35 e = GrabbingEntry(root)
36 e.grid(row=1, column=2)
37
38 w = Label(root, text="Egg")
39 w.grid(row=2, column=1)
40
41 e = GrabbingEntry(root)
42 e.grid(row=2, column=2)
43
44 #root.bind("<Escape>", lambda e: root.quit())
45
46 mainloop()
47
1 # tk032
2 # scrolled frame
3 #
4 # fredrik lundh, april 1998 (derived from material in "Instant Python")
5 #
6 # fredrik@pythonware.com
7 # http://www.pythonware.com
8 #
9
10 from Tkinter import *
11
12 class ScrolledFrame(Frame):
13
14 def __init__(self, master, **kw):
15 apply(Frame.__init__, (self, master), kw)
16
17 # create the widgets
18 scrollbar = Scrollbar(self, orient=VERTICAL)
19
20 self.canvas = Canvas(
21 self,
22 bd=0, highlightthickness=0,
23 yscrollcommand=scrollbar.set,
24 bg="red"
25 )
26
27 scrollbar.config(command=self.canvas.yview)
28
29 scrollbar.pack(fill=Y, side=RIGHT)
30 self.canvas.pack(expand=1, fill=BOTH, side=LEFT, )
31
32 # reset the view
33 # (always do this if you don't use scrollbars)
34 self.canvas.xview("moveto", 0)
35 self.canvas.yview("moveto", 0)
36
37 # create the inner frame
38 self.inner = Frame(self.canvas, bg="blue")
39
40 # track changes to its size
41 self.inner.bind('<Configure>', self.__configure)
42
43 # place the frame inside the canvas (this also
44 # runs the __configure method)
45 self.canvas.create_window(0, 0, window=self.inner, anchor=NW)
46
47 def __configure(self, event):
48
49 # update the scrollbars to match the size of the inner frame
50 size = self.inner.winfo_reqwidth(), self.inner.winfo_reqheight()
51 self.canvas.config(scrollregion="0 0 %s %s" % size)
52
53 # --------------------------------------------------------------------
54 # test stuff
55
56 root = Tk()
57
58 f = ScrolledFrame(
59 root,
60 bd=2, relief=SUNKEN,
61 highlightthickness=2
62 )
63 f.pack(expand=1, fill=BOTH)
64
65 t = Text(
66 f.inner,
67 bg="gold",
68 height=100, width=40,
69 bd=0, highlightthickness=0
70 )
71 t.pack(expand=1, fill=BOTH)
72
73 for i in range(100):
74 t.insert(END, "line %d\n" % (i+1))
75
76 root.mainloop()
77