猫史档案馆


【Python作品分享】外星人【作品秀】

用户:刘奕恺kevin刘奕恺kevin查看:0 回复:0 评论:0 创建时间:2022-12-20T21:32:05


【作品展示】

center_image

 

【作品介绍】

一只外星人

 

【作品源代码】

from tkinter import *


window = Tk()
window.title('外星人')
c = Canvas(window, height=300, width=400)
c.pack()
body = c.create_oval(100, 150, 300, 250, fill='green')
eye = c.create_oval(170, 70, 230, 130, fill='white')
eyeball = c.create_oval(190, 90, 210, 110, fill='black')
mouth = c.create_oval(150, 220, 250, 240, fill='black')
neck = c.create_line(200, 150, 200, 130)
hat = c.create_polygon(180, 75, 220, 75, 200, 20, fill='blue')
# 画出各个部分


def 张嘴(event):
    c.itemconfig(mouth, fill='black')


def 张嘴2():
    c.itemconfig(mouth, fill='black')


def 闭嘴(event):
    c.itemconfig(mouth, fill='green')


def 闭眼(event):
    c.itemconfig(eye, fill='green')
    c.itemconfig(eyeball, state=HIDDEN)


def 睁眼(event):
    c.itemconfig(eye, fill='white')
    c.itemconfig(eyeball, state=NORMAL)


words = c.create_text(200, 280, text='我是外星人!')


def 偷帽子(event):
    c.itemconfig(hat, state=HIDDEN)
    c.itemconfig(words, text='还我帽子!')


window.attributes('-topmost', 1)


def 打嗝(event):
    张嘴2()
    c.itemconfig(words, text='嗝!')


def 动眼(event):
    key = event.keysym
    if key == 'Up':
        c.move(eyeball, 0, -10)
    elif key == 'Down':
        c.move(eyeball, 0, 10)
    elif key == 'Left':
        c.move(eyeball, -10, 0)
    elif key == 'Right':
        c.move(eyeball, 10, 0)


def 还原(event):
    c.itemconfig(words, text='我是外星人!')
    c.itemconfig(hat, state=NORMAL)


c.bind_all('', 动眼)
c.bind_all('<2>', 打嗝)
c.bind_all('', 睁眼)
c.bind_all('', 闭眼)
c.bind_all('', 偷帽子)
c.bind_all('<1>', 张嘴)
c.bind_all('<3>', 闭嘴)
c.bind_all('', 还原)
window.mainloop()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页