tkinter:画布 组件.2021-01-11

from tkinter import *

window = Tk()
window.title('我的窗口-tkinter画布范例')
window.geometry('480x640')
# 创建主窗口

c = Canvas(window, bg='blue', height=100, width=200)
c.pack()
# 创建画布控件.背景蓝色,高100,宽200

image_file = PhotoImage(file='favicon32.PNG')
image_ = c.create_image(10, 10, anchor='nw', image=image_file)
# 放入图片 到 画布部件 中.锚定点为图片的最西北点,坐标为(10,10)
# tkinter.PhotoImage()目前仅支持 GIF and PGM/PPM 等少数文件格式.不同python版本有差异

c.pack()
# 放入画布控件到窗口中

x0, y0, x1, y1 = 50, 50, 80, 80
line = c.create_line(x0, y0, x1, y1)
# 画布上画一条平凡无奇的线(坐标(50,50)到(80,80)画一条直线)

oval = c.create_oval(x0, y0, x1, y1, fill='red')
# 画布上画一个圆,填充色为红色

arc = c.create_arc(x0 + 30, y0 + 30, x1 + 30, y1 + 30, start=0, extent=180)
# 画布上画一个扇形,从0度到180度

rect = c.create_rectangle(100, 30, 100 + 20, 30 + 20)
# 画布上画一个矩形


def moveit():
    c.move(rect, 0, 8)

b = Button(window, text='移动', command=moveit)
b.pack()
# 创建一个按钮.按下后触发移动功能

window.mainloop()
# 主循环

图片素材:  https://g.csdnimg.cn/static/logo/favicon32.ico

运行效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mklpo147

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值