Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1 | # tst.py |
| 2 | from Tkinter import * |
| 3 | import sys |
| 4 | |
| 5 | def do_hello(): |
| 6 | print 'Hello world!' |
| 7 | |
| 8 | class Quit(Button): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 9 | def __init__(self, master=None, cnf={}): |
| 10 | Button.__init__(self, master, |
Guido van Rossum | 639b8b8 | 1994-07-06 09:27:00 +0000 | [diff] [blame] | 11 | ({'name': 'quit', |
| 12 | 'text': 'Quit', |
| 13 | 'command': self.quit}, |
| 14 | cnf)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 15 | |
| 16 | class Stuff(Canvas): |
| 17 | def enter(self, e): |
| 18 | print 'Enter' |
| 19 | self.itemconfig('current', {'fill': 'red'}) |
| 20 | def leave(self, e): |
| 21 | print 'Leave' |
| 22 | self.itemconfig('current', {'fill': 'blue'}) |
| 23 | def __init__(self, master=None, cnf={}): |
| 24 | Canvas.__init__(self, master, |
| 25 | {'width': 100, 'height': 100}) |
| 26 | Canvas.config(self, cnf) |
| 27 | self.create_rectangle(30, 30, 70, 70, |
Guido van Rossum | e757185 | 1994-06-20 11:36:09 +0000 | [diff] [blame] | 28 | {'fill': 'blue', 'tags': 'box'}) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 29 | Canvas.bind(self, 'box', '<Enter>', self.enter) |
| 30 | Canvas.bind(self, 'box', '<Leave>', self.leave) |
| 31 | |
| 32 | class Test(Frame): |
| 33 | text = 'Testing' |
| 34 | num = 1 |
| 35 | def do_xy(self, e): |
| 36 | print (e.x, e.y) |
| 37 | def do_test(self): |
| 38 | if not self.num % 10: |
| 39 | self.text = 'Testing 1 ...' |
| 40 | self.text = self.text + ' ' + `self.num` |
| 41 | self.num = self.num + 1 |
| 42 | self.testing['text'] = self.text |
| 43 | def do_err(self): |
| 44 | 1/0 |
| 45 | def do_after(self): |
| 46 | self.testing.invoke() |
| 47 | self.after(10000, self.do_after) |
| 48 | def __init__(self, master=None): |
| 49 | Frame.__init__(self, master) |
| 50 | self['bd'] = 30 |
| 51 | Pack.config(self) |
| 52 | self.bind('<Motion>', self.do_xy) |
| 53 | self.hello = Button(self, {'name': 'hello', |
| 54 | 'text': 'Hello', |
| 55 | 'command': do_hello, |
| 56 | Pack: {'fill': 'both'}}) |
| 57 | self.testing = Button(self) |
| 58 | self.testing['text'] = self.text |
| 59 | self.testing['command'] = self.do_test |
| 60 | Pack.config(self.testing, {'fill': 'both'}) |
| 61 | self.err = Button(self, {'text': 'Error', |
| 62 | 'command': self.do_err, |
| 63 | Pack: {'fill': 'both'}}) |
| 64 | self.quit = Quit(self, {Pack: {'fill': 'both'}}) |
| 65 | self.exit = Button(self, |
| 66 | {'text': 'Exit', |
| 67 | 'command': lambda: sys.exit(0), |
| 68 | Pack: {'fill': 'both'}}) |
| 69 | self.stuff = Stuff(self, {Pack: {'padx': 2, 'pady': 2}}) |
| 70 | self.do_after() |
| 71 | |
| 72 | test = Test() |
| 73 | test.master.title('Tkinter Test') |
| 74 | test.master.iconname('Test') |
| 75 | test.master.maxsize(500, 500) |
| 76 | test.testing.invoke() |
| 77 | |
| 78 | # Use the -i option and type ^C to get a prompt |
Guido van Rossum | 639b8b8 | 1994-07-06 09:27:00 +0000 | [diff] [blame] | 79 | mainloop() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 80 | |