Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | |
| 3 | # this file demonstrates the creation of widgets as part of a canvas object |
| 4 | |
| 5 | class Test(Frame): |
| 6 | def printhi(self): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 7 | print("hi") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 8 | |
| 9 | def createWidgets(self): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 10 | self.QUIT = Button(self, text='QUIT', foreground='red', |
| 11 | command=self.quit) |
| 12 | self.QUIT.pack(side=BOTTOM, fill=BOTH) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 13 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 14 | self.draw = Canvas(self, width="5i", height="5i") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 15 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 16 | self.button = Button(self, text="this is a button", |
| 17 | command=self.printhi) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 18 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 19 | # note here the coords are given in pixels (form the |
| 20 | # upper right and corner of the window, as usual for X) |
| 21 | # but might just have well been given in inches or points or |
| 22 | # whatever...use the "anchor" option to control what point of the |
| 23 | # widget (in this case the button) gets mapped to the given x, y. |
| 24 | # you can specify corners, edges, center, etc... |
| 25 | self.draw.create_window(300, 300, window=self.button) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 26 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 27 | self.draw.pack(side=LEFT) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 28 | |
| 29 | def __init__(self, master=None): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 30 | Frame.__init__(self, master) |
| 31 | Pack.config(self) |
| 32 | self.createWidgets() |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 33 | |
| 34 | test = Test() |
| 35 | |
| 36 | test.mainloop() |