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