python學習 GUI第7節

canvas組件 :
Python Tkinter 畫布(Canvas)組件和 html5 中的畫布一樣,都是用來繪圖的。您可以將圖形,文本,小部件或框架放置在畫布上

語法格式如下:
w = Canvas ( master, option=value, … )

from tkinter import *

root = Tk()
w = Canvas(root,width=200,height=100)
w.pack()
w.create_line(0,50,200,50,fill="yellow")#line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)
w.create_line(100,0,100,100,fill="red",dash=(4,4))# 創建一個長方形
w.create_rectangle(50,25,150,75,fill="blue"))# 創建一個長方形

mainloop()

運行結果如下
在這裏插入圖片描述
多邊形
在這裏插入圖片描述
接下來做一個畫板:

from tkinter import *

root = Tk()
w = Canvas(root,width=400,height=200)
w.pack()

def paint(event):
    x1,y1=(event.x-1,event.y-1)
    x2,y2=(event.x+1,event.y+1)
    w.create_oval(x1,y1,x2,y2,fill="red")#定義畫的點爲一個極小圓

w.bind("<B1-Motion>",paint)
mainloop()

結果如下
在這裏插入圖片描述

在這裏插入圖片描述
canvas是一個真心強大的組件
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章