猫史档案馆


【Python作品分享】宠物

用户:烙阎s烙阎s查看:0 回复:0 评论:0 创建时间:2022-06-27T09:38:30


【作品展示】

center_image

 

【作品介绍】

我上一个作品的升级版

 

【作品源代码】

from tkinter import HIDDEN,NORMAL,Tk,Canvas


def toggle_eyes():
    current_color=c.itemcget(eye_left,'fill')
    new_color=c.body_color if current_color=='white' else 'white'
    current_state=c.itemcget(pupil_left,'state')
    new_state=NORMAL if current_state==HIDDEN else HIDDEN
    c.itemconfigure(pupil_left,state=new_state)
    c.itemconfigure(pupil_right,state=new_state)
    c.itemconfigure(eye_left,fill=new_color)
    c.itemconfigure(eye_right,fill=new_color)


def blink():
    toggle_eyes()
    root.after(500,toggle_eyes)
    root.after(3000,blink)

    
def show_happy(event):
    if(20<=event.x<=350)and(20<=event.y<=350):
        c.itemconfigure(cheek_left,state=NORMAL)
        c.itemconfigure(cheek_right,state=NORMAL)
        c.itemconfigure(mouth_happy,state=NORMAL)
        c.itemconfigure(mouth_normal,state=HIDDEN)
        c.itemconfigure(mouth_sad,state=HIDDEN)
    return

def hide_happy(event):
    c.itemconfigure(cheek_left,state=HIDDEN)
    c.itemconfigure(cheek_right,state=HIDDEN)
    c.itemconfigure(mouth_happy,state=HIDDEN)
    c.itemconfigure(mouth_normal,state=NORMAL)
    c.itemconfigure(mouth_sad,state=HIDDEN)
    return
root=Tk()
c=Canvas(root,width=400,height=400)
c.configure(bg='dark blue',highlightthickness=0)
c.body_color='Skyblue1'
body=c.create_oval(35,20,365,350,outline=c.body_color,fill=c.body_color)
ear_left=c.create_polygon(75,80,75,10,320,70,outline=c.body_color,fill=c.body_color)
ear_right=c.create_polygon(225,45,325,10,320,70,outline=c.body_color,fill=c.body_color)


foot_left=c.create_oval(65,320,145,360,outline=c.body_color,fill=c.body_color)
foot_right=c.create_oval(250,320,330,360,outline=c.body_color,fill=c.body_color)


eye_left=c.create_oval(130,110,160,170,outline='black',fill='white')
pupil_left=c.create_oval(140,145,150,155,outline='black',fill='black')
eye_right=c.create_oval(230,110,260,170,outline='black',fill='white')
pupil_right=c.create_oval(240,145,250,155,outline='black',fill='black')


mouth_normal=c.create_line(170,250,200,272,230,250,喵ooth=1,width=2,state=NORMAL)
mouth_happy=c.create_line(170,250,200,282,230,250,喵ooth=1,width=2,state=HIDDEN)
mouth_sad=c.create_line(170,250,200,232,230,250,喵ooth=1,width=2,state=HIDDEN)


cheek_left=c.create_oval(70,180,120,230,outline='pink',fill='pink',state=HIDDEN)
cheek_right=c.create_oval(280,180,330,230,outline='pink',fill='pink',state=HIDDEN)


c.pack()


c.bind('',show_happy)
c.bind('',hide_happy)
root.after(1000,blink)    
root.mainloop()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页