blob: 2645e57a37e4c5d6e9070f2c9a8ee843fc7006b6 [file] [log] [blame]
Guido van Rossumd5f03801999-01-11 14:45:11 +00001from Tkinter import *
2
3class Separator:
4
5 def __init__(self, master, orient, min=10, thickness=5, bg=None):
6 self.min = max(1, min)
7 self.thickness = max(1, thickness)
8 if orient in ("h", "horizontal"):
9 self.side = "left"
10 self.dim = "width"
11 self.dir = "x"
12 self.cursor = "sb_h_double_arrow"
13 elif orient in ("v", "vertical"):
14 self.side = "top"
15 self.dim = "height"
16 self.dir = "y"
17 self.cursor = "sb_v_double_arrow"
18 else:
19 raise ValueError, "Separator: orient should be h or v"
20 self.winfo_dim = "winfo_" + self.dim
21 self.master = master = Frame(master)
22 master.pack(expand=1, fill="both")
23 self.f1 = Frame(master)
24 self.f1.pack(expand=1, fill="both", side=self.side)
25 self.div = Frame(master, cursor=self.cursor)
26 self.div[self.dim] = self.thickness
27 self.div.pack(fill="both", side=self.side)
28 self.f2 = Frame(master)
29 self.f2.pack(expand=1, fill="both", side=self.side)
30 self.div.bind("<ButtonPress-1>", self.divider_press)
31 if bg:
32 ##self.f1["bg"] = bg
33 ##self.f2["bg"] = bg
34 self.div["bg"] = bg
35
36 def parts(self):
37 return self.f1, self.f2
38
39 def divider_press(self, event):
40 self.press_event = event
41 self.f1.pack_propagate(0)
42 self.f2.pack_propagate(0)
43 for f in self.f1, self.f2:
44 for dim in "width", "height":
45 f[dim] = getattr(f, "winfo_"+dim)()
46 self.div.bind("<Motion>", self.div_motion)
47 self.div.bind("<ButtonRelease-1>", self.div_release)
Guido van Rossum5cc31291999-01-12 22:09:18 +000048 self.div.grab_set()
Guido van Rossumd5f03801999-01-11 14:45:11 +000049
50 def div_motion(self, event):
51 delta = getattr(event, self.dir) - getattr(self.press_event, self.dir)
52 if delta:
53 dim1 = getattr(self.f1, self.winfo_dim)()
54 dim2 = getattr(self.f2, self.winfo_dim)()
55 delta = max(delta, self.min-dim1)
56 delta = min(delta, dim2-self.min)
57 dim1 = dim1 + delta
58 dim2 = dim2 - delta
59 self.f1[self.dim] = dim1
60 self.f2[self.dim] = dim2
61
62 def div_release(self, event):
63 self.div_motion(event)
64 self.div.unbind("<Motion>")
Guido van Rossum5cc31291999-01-12 22:09:18 +000065 self.div.grab_release()
Guido van Rossumd5f03801999-01-11 14:45:11 +000066
67class VSeparator(Separator):
68
69 def __init__(self, master, min=10, thickness=5, bg=None):
70 Separator.__init__(self, master, "v", min, thickness, bg)
71
72class HSeparator(Separator):
73
74 def __init__(self, master, min=10, thickness=5, bg=None):
75 Separator.__init__(self, master, "h", min, thickness, bg)
76
77def main():
78 root = Tk()
79 tlist = []
80 outer = HSeparator(root, bg="red")
81 for part in outer.parts():
82 inner = VSeparator(part, bg="blue")
83 for f in inner.parts():
84 t = Text(f, width=40, height=10, borderwidth=0)
85 t.pack(fill="both", expand=1)
86 tlist.append(t)
87 tlist[0].insert("1.0", "Make your own Mondrian!")
88 tlist[1].insert("1.0", "Move the colored dividers...")
Guido van Rossum5cc31291999-01-12 22:09:18 +000089 root.mainloop()
Guido van Rossumd5f03801999-01-11 14:45:11 +000090
91if __name__ == '__main__':
92 main()