Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 3 | # this is the same as simple-demo-1.py, but uses |
| 4 | # subclassing. |
| 5 | # note that there is no explicit call to start Tk. |
| 6 | # Tkinter is smart enough to start the system if it's not already going. |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 7 | |
| 8 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 9 | class Test(Frame): |
| 10 | def printit(self): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 11 | print("hi") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 12 | |
| 13 | def createWidgets(self): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 14 | self.QUIT = Button(self, text='QUIT', foreground='red', |
| 15 | command=self.quit) |
| 16 | self.QUIT.pack(side=BOTTOM, fill=BOTH) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 17 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 18 | self.draw = Canvas(self, width="5i", height="5i") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 19 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 20 | self.speed = Scale(self, orient=HORIZONTAL, from_=-100, to=100) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 21 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 22 | self.speed.pack(side=BOTTOM, fill=X) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 23 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 24 | # all of these work.. |
| 25 | self.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="blue") |
| 26 | self.draw.pack(side=LEFT) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 27 | |
| 28 | def moveThing(self, *args): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 29 | velocity = self.speed.get() |
| 30 | str = float(velocity) / 1000.0 |
| 31 | str = "%ri" % (str,) |
| 32 | self.draw.move("thing", str, str) |
| 33 | self.after(10, self.moveThing) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 35 | def __init__(self, master=None): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 36 | Frame.__init__(self, master) |
| 37 | Pack.config(self) |
| 38 | self.createWidgets() |
| 39 | self.after(10, self.moveThing) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | test = Test() |
| 43 | |
| 44 | test.mainloop() |