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
运行效果: