|
| 1 | +from tkinter import Tk , HIDDEN , NORMAL , Canvas |
| 2 | + |
| 3 | +def toggle_eyes(): |
| 4 | + current_color = c.itemcget(eye_left,'fill') |
| 5 | + new_color = c.body_color if current_color == 'white' else 'white' |
| 6 | + current_state = c.itemcget(pupil_left , 'state') |
| 7 | + new_state = NORMAL if current_state == HIDDEN else HIDDEN |
| 8 | + c.itemconfigure(pupil_left , state = new_state) |
| 9 | + c.itemconfigure(pupil_right , state = new_state) |
| 10 | + c.itemconfigure(eye_left , fill = new_color) |
| 11 | + c.itemconfigure(eye_right , fill = new_color) |
| 12 | + |
| 13 | +def blink(): |
| 14 | + toggle_eyes() |
| 15 | + win.after(250,toggle_eyes) |
| 16 | + win.after(3000,blink) |
| 17 | + |
| 18 | +def toggle_pupils(): |
| 19 | + if not c.crossed_eyes: |
| 20 | + c.move(pupil_left , 10,-5) |
| 21 | + c.move(pupil_right , -10,-5) |
| 22 | + c.crossed_eyes = True |
| 23 | + else: |
| 24 | + c.move(pupil_left , -10,5) |
| 25 | + c.move(pupil_right , 10,5) |
| 26 | + c.crossed_eyes = False |
| 27 | + |
| 28 | +def toggle_tongue(): |
| 29 | + if not c.tonque_out: |
| 30 | + c.itemconfigure(tongue_tip , state = NORMAL) |
| 31 | + c.itemconfigure(tongue_main , state = NORMAL) |
| 32 | + c.tonque_out = True |
| 33 | + else: |
| 34 | + c.itemconfigure(tongue_tip , state = HIDDEN) |
| 35 | + c.itemconfigure(tongue_main , state = HIDDEN) |
| 36 | + c.tonque_out = False |
| 37 | + |
| 38 | +def cheeky(event): |
| 39 | + toggle_tongue() |
| 40 | + toggle_pupils() |
| 41 | + hide_happy(event) |
| 42 | + win.after(1000,toggle_tongue) |
| 43 | + win.after(1000,toggle_pupils) |
| 44 | + return |
| 45 | + |
| 46 | +def show_happy(event): |
| 47 | + if(20<= event.x and event.x <= 350) and (20<= event.y and event.y <= 350): |
| 48 | + c.itemconfigure(cheek_left , state = NORMAL) |
| 49 | + c.itemconfigure(cheek_right , state = NORMAL) |
| 50 | + c.itemconfigure(mouth_happy , state = NORMAL) |
| 51 | + c.itemconfigure(mouth_normal , state = HIDDEN) |
| 52 | + c.itemconfigure(mouth_sad, state = HIDDEN) |
| 53 | + c.happy_level = 10 |
| 54 | + return |
| 55 | + |
| 56 | +def hide_happy(event): |
| 57 | + c.itemconfigure(cheek_left , state = HIDDEN) |
| 58 | + c.itemconfigure(cheek_right , state = HIDDEN) |
| 59 | + c.itemconfigure(mouth_happy , state = HIDDEN) |
| 60 | + c.itemconfigure(mouth_normal , state = NORMAL) |
| 61 | + c.itemconfigure(mouth_sad, state = HIDDEN) |
| 62 | + return |
| 63 | + |
| 64 | +def sad(): |
| 65 | + if c.happy_level == 0 : |
| 66 | + c.itemconfigure(mouth_happy , state = HIDDEN) |
| 67 | + c.itemconfigure(mouth_normal , state = HIDDEN) |
| 68 | + c.itemconfigure(mouth_sad , state = NORMAL) |
| 69 | + else: |
| 70 | + c.happy_level -= 1 |
| 71 | + win.after(500,sad) |
| 72 | + |
| 73 | +win = Tk() |
| 74 | + |
| 75 | +c = Canvas(win , width=400 , height=400) |
| 76 | +c.configure(bg='dark blue' , highlightthickness=0) |
| 77 | + |
| 78 | +c.body_color = 'SkyBlue1' |
| 79 | + |
| 80 | +body = c.create_oval(35,20,365,350,outline=c.body_color , fill=c.body_color) |
| 81 | +foot_left = c.create_oval(65,320,145,360 , outline=c.body_color , fill=c.body_color) |
| 82 | +foot_right = c.create_oval(250,320,330,360 , outline=c.body_color , fill=c.body_color) |
| 83 | + |
| 84 | +ear_left = c.create_polygon(75,80,75,10,165,70,outline=c.body_color , fill=c.body_color) |
| 85 | +ear_right = c.create_polygon(255,45,325,10,320,70,outline=c.body_color , fill=c.body_color) |
| 86 | + |
| 87 | +eye_left = c.create_oval(130,110,160,170,outline='black' , fill='white') |
| 88 | +pupil_left = c.create_oval(140,145,150,155,outline='black' , fill='black') |
| 89 | +eye_right = c.create_oval(230,110,260,170,outline='black' , fill='white') |
| 90 | +pupil_right = c.create_oval(240,145,250,155,outline='black' , fill='black') |
| 91 | + |
| 92 | +mouth_normal = c.create_line(170,250,200,272,230,250,smooth=1 , width=2 , state=NORMAL) |
| 93 | +mouth_happy = c.create_line(170,250,200,282,230,250,smooth=1 , width=2 , state=HIDDEN) |
| 94 | +mouth_sad = c.create_line(170,250,200,232,230,250,smooth=1 , width=2 , state=HIDDEN) |
| 95 | + |
| 96 | +tongue_main = c.create_rectangle(170,250,230,290,outline='red' , fill='red',state=HIDDEN) |
| 97 | +tongue_tip = c.create_oval(170,285,230,300,outline='red' , fill='red',state=HIDDEN) |
| 98 | + |
| 99 | +cheek_left = c.create_oval(70,180,120,230,outline='pink' , fill='pink',state=HIDDEN) |
| 100 | +cheek_right = c.create_oval(280,180,330,230,outline='pink' , fill='pink',state=HIDDEN) |
| 101 | + |
| 102 | +c.pack() |
| 103 | + |
| 104 | +c.bind('<Motion>' , show_happy) |
| 105 | +c.bind('<Leave>' , hide_happy) |
| 106 | +c.bind('<Double-1>' , cheeky) |
| 107 | + |
| 108 | +c.crossed_eyes = False |
| 109 | +c.tonque_out = False |
| 110 | +c.happy_level = 10 |
| 111 | + |
| 112 | +win.after(1000,blink) |
| 113 | +win.after(5000,sad) |
| 114 | +win.mainloop() |
0 commit comments