tkinter 07 Canvas 画布

  •  
    运行结果:https://s1.ax1x.com/2018/04/16/Ce7JOK.gif
  • # coding=gbk
    # 图片地址:https://morvanzhou.github.io/static/results/tkinter/ins.gif
    # 运行结果:https://s1.ax1x.com/2018/04/16/Ce7JOK.gif
    import tkinter as tk
    
    window = tk.Tk()
    window.title('my window')
    window.geometry('200x200')
    
    canvas = tk.Canvas(window, bg='blue', height=100, width=200)#canvas画布
    image_file = tk.PhotoImage(file='ins.gif')
    image = canvas.create_image(10, 10, # 设定图片放置的位置
                                anchor='nw',# 把图片的左上角作为锚定点(理解为坐标原点)
                                image=image_file)
    x0, y0, x1, y1= 50, 50, 80, 80
    line = canvas.create_line(x0, y0, x1, y1)# 绘制直线
    oval = canvas.create_oval(x0, y0, x1, y1, fill='red')# 绘制圆
    # 参数start=0和extent=180,其实就是从0度到180度,就好像扇子的边打开一样。在我们看来就是个半圆,
    # 如果改为extent=90,我们看到的就是一个1/4圆
    arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=0, extent=180)#绘制弧
    rect = canvas.create_rectangle(100, 30, 100+20, 30+20)# 绘制方框
    canvas.pack()
    
    # 横坐标移动0个单位,纵坐标移动2个单位
    # 注意:左上角为坐标原点
    def moveit():
        canvas.move(rect, 0, 2)
    
    b = tk.Button(window, text='move', command=moveit).pack()
    
    
    window.mainloop()

猜你喜欢

转载自www.cnblogs.com/jkn1234/p/8856955.html