Guido van Rossum | d5f0380 | 1999-01-11 14:45:11 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | |
| 3 | class Separator: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | d5f0380 | 1999-01-11 14:45:11 +0000 | [diff] [blame] | 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" |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 13 | elif orient in ("v", "vertical"): |
| 14 | self.side = "top" |
| 15 | self.dim = "height" |
| 16 | self.dir = "y" |
Guido van Rossum | d5f0380 | 1999-01-11 14:45:11 +0000 | [diff] [blame] | 17 | self.cursor = "sb_v_double_arrow" |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 18 | else: |
| 19 | raise ValueError, "Separator: orient should be h or v" |
| 20 | self.winfo_dim = "winfo_" + self.dim |
Guido van Rossum | d5f0380 | 1999-01-11 14:45:11 +0000 | [diff] [blame] | 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 Rossum | 5cc3129 | 1999-01-12 22:09:18 +0000 | [diff] [blame] | 48 | self.div.grab_set() |
Guido van Rossum | d5f0380 | 1999-01-11 14:45:11 +0000 | [diff] [blame] | 49 | |
| 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 Rossum | 5cc3129 | 1999-01-12 22:09:18 +0000 | [diff] [blame] | 65 | self.div.grab_release() |
Guido van Rossum | d5f0380 | 1999-01-11 14:45:11 +0000 | [diff] [blame] | 66 | |
| 67 | class VSeparator(Separator): |
| 68 | |
| 69 | def __init__(self, master, min=10, thickness=5, bg=None): |
| 70 | Separator.__init__(self, master, "v", min, thickness, bg) |
| 71 | |
| 72 | class HSeparator(Separator): |
| 73 | |
| 74 | def __init__(self, master, min=10, thickness=5, bg=None): |
| 75 | Separator.__init__(self, master, "h", min, thickness, bg) |
| 76 | |
| 77 | def 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 Rossum | 5cc3129 | 1999-01-12 22:09:18 +0000 | [diff] [blame] | 89 | root.mainloop() |
Guido van Rossum | d5f0380 | 1999-01-11 14:45:11 +0000 | [diff] [blame] | 90 | |
| 91 | if __name__ == '__main__': |
| 92 | main() |