Python編程tkinter庫Canvas實現塗鴉顏色表及圍棋盤示例
tkinter庫Canvas操作三個實例
實例一:塗鴉
import tkinter as tk import pyautogui as ag from time import sleep def paint(event): x1, y1 = (event.x - 1), (event.y - 1) x2, y2 = (event.x + 1), (event.y + 1) tCanvas.create_oval(x1, y1, x2, y2, fill = "black") def moveRect(w,h): ag.moveRel(w,0, duration=w/1000) # moveRel移動 # dragRel拖曳 ag.moveRel(0,h, duration=h/1000) ag.moveRel(-w,0, duration=w/1000) ag.moveRel(0,-h, duration=h/1000) sleep(0.5) win = tk.Tk() X,Y=ag.size() winPos = "640x480+" winPos += str((X-640)//2)+"+" winPos += str((Y-480)//2) win.geometry(winPos) win.resizable(False, False) win.title('桌面分辨率:'+str(X)+"x"+str(Y)) win.update() tCanvas = tk.Canvas(win, width = win.winfo_width(), height = 400, bg = 'white') tCanvas.pack(side = "top") tCanvas.bind("<B1-Motion>", paint) tCanvas.update() tk.Label(win, text = "畫佈作圖").pack(side = "bottom") x0,y0 = 150,60 w,h = 350,160 x , y = win.winfo_rootx()+x0, win.winfo_rooty()+y0 ag.moveTo(x, y, duration=0.5) moveRect(w,h) #畫直線 tCanvas.create_line(x0, y0, x0+w, y0) tCanvas.create_line(x0+w, y0, x0+w, y0+h) #可以把左上和右下兩個坐標點一起賦值給變量 coord = x0+w, y0+h, x0, y0+h tCanvas.create_line(coord) coord = x0, y0+h, x0, y0 tCanvas.create_line(coord, fill = 'blue') tCanvas.update() moveRect(w-h/3,h) #畫矩形 coord = x0+1,y0+1,x0+w-1-h/3,y0+h-1 tCanvas.create_rectangle(coord,fill='limegreen') tCanvas.update() moveRect(h,h) #畫圓或橢圓 coord = x0,y0,x0+h,y0+h tCanvas.create_oval(coord,fill='deepskyblue') tCanvas.update() x0=x0+h/2 ag.moveRel(h/2,0) moveRect(w-h/2,h) coord = x0,y0,x0+w-h/2,y0+h tCanvas.create_oval(coord,fill='deeppink') tCanvas.update() #畫扇形、弓形或一段弧 #type = PIESLICE 扇形、CHORD 弓形、ARC 弧形 sleep(1) coord = 100, 100, 300, 300 tCanvas.create_arc(coord,start=0,extent=180,fill='gold') tCanvas.update() sleep(1) coord = 240, 60, 490, 310 #顏色也可用6位16進制數的GRB值表示 tCanvas.create_arc(coord,style=tk.CHORD,start=10,fill='#7CFC00') tCanvas.update() sleep(1) coord = 180, 10, 470, 300 tCanvas.create_arc(coord,style=tk.ARC,width=3,dash=6,extent=-180) #參數有很多,可以上網搜相關參數表 win.mainloop()
運行效果圖:
註:程序運行時,按下鼠標左鍵就可以在畫佈上作圖,上圖 python 字樣就是本人所畫 ^_^
其它作圖函數:
# 多邊形、三角形 tCanvas.create_polygon((10,100,10,180,100,180),fill='blue') # 文字 tCanvas.create_text((150,50),text=u'輸出文字',anchor=E) # 位圖 bmap={1:'error',2:'info',3:'question',4:'hourglass'} for i in bmap: tCanvas.create_bitmap((20*i,20),bitmap=bmap[i]) # 調入圖片 img=PhotoImage(file='D:/pictures/exam.gif') tCanvas.create_image((200,200),image=img) tCanvas.pack()
實例二:顏色表
import tkinter as tk import pyautogui as ag win = tk.Tk() X,Y=ag.size() W,H=1200,800 winPos = str(W)+"x"+str(H)+"+" winPos += str((X-W)//2)+"+" winPos += str((Y-H)//2) win.geometry(winPos) win.resizable(False, False) win.title('桌面分辨率:'+str(X)+"x"+str(Y)+' '*6+'窗口大小:'+str(W)+"x"+str(H)) win.update() colorTuple=( ('black', '#000000'), ('navy', '#000080'), ('darkblue', '#00008B'), ('mediumblue', '#0000CD'), ('blue', '#0000FF'), ('darkgreen', '#006400'), ('green', '#008000'), ('teal', '#008080'), ('darkcyan', '#008B8B'), ('deepskyblue', '#00BFFF'), ('darkturquoise', '#00CED1'), ('mediumspringgreen', '#00FA9A'), ('lime', '#00FF00'), ('springgreen', '#00FF7F'), ('aqua', '#00FFFF'), ('cyan', '#00FFFF'), ('midnightblue', '#191970'), ('dodgerblue', '#1E90FF'), ('lightseagreen', '#20B2AA'), ('forestgreen', '#228B22'), ('seagreen', '#2E8B57'), ('darkslategray', '#2F4F4F'), ('limegreen', '#32CD32'), ('mediumseagreen', '#3CB371'), ('turquoise', '#40E0D0'), ('royalblue', '#4169E1'), ('steelblue', '#4682B4'), ('darkslateblue', '#483D8B'), ('mediumturquoise', '#48D1CC'), ('indigo', '#4B0082'), ('darkolivegreen', '#556B2F'), ('cadetblue', '#5F9EA0'), ('cornflowerblue', '#6495ED'), ('mediumaquamarine', '#66CDAA'), ('dimgray', '#696969'), ('slateblue', '#6A5ACD'), ('olivedrab', '#6B8E23'), ('slategray', '#708090'), ('lightslategray', '#778899'), ('mediumslateblue', '#7B68EE'), ('lawngreen', '#7CFC00'), ('chartreuse', '#7FFF00'), ('aquamarine', '#7FFFD4'), ('maroon', '#800000'), ('purple', '#800080'), ('olive', '#808000'), ('gray', '#808080'), ('skyblue', '#87CEEB'), ('lightskyblue', '#87CEFA'), ('blueviolet', '#8A2BE2'), ('darkred', '#8B0000'), ('darkmagenta', '#8B008B'), ('saddlebrown', '#8B4513'), ('darkseagreen', '#8FBC8F'), ('lightgreen', '#90EE90'), ('mediumpurple', '#9370DB'), ('darkviolet', '#9400D3'), ('palegreen', '#98FB98'), ('darkorchid', '#9932CC'), ('yellowgreen', '#9ACD32'), ('sienna', '#A0522D'), ('brown', '#A52A2A'), ('darkgray', '#A9A9A9'), ('lightblue', '#ADD8E6'), ('greenyellow', '#ADFF2F'), ('paleturquoise', '#AFEEEE'), ('lightsteelblue', '#B0C4DE'), ('powderblue', '#B0E0E6'), ('firebrick', '#B22222'), ('darkgoldenrod', '#B8860B'), ('mediumorchid', '#BA55D3'), ('rosybrown', '#BC8F8F'), ('darkkhaki', '#BDB76B'), ('silver', '#C0C0C0'), ('mediumvioletred', '#C71585'), ('indianred', '#CD5C5C'), ('peru', '#CD853F'), ('chocolate', '#D2691E'), ('tan', '#D2B48C'), ('lightgray', '#D3D3D3'), ('thistle', '#D8BFD8'), ('orchid', '#DA70D6'), ('goldenrod', '#DAA520'), ('palevioletred', '#DB7093'), ('crimson', '#DC143C'), ('gainsboro', '#DCDCDC'), ('plum', '#DDA0DD'), ('burlywood', '#DEB887'), ('lightcyan', '#E0FFFF'), ('lavender', '#E6E6FA'), ('darksalmon', '#E9967A'), ('violet', '#EE82EE'), ('palegoldenrod', '#EEE8AA'), ('lightcoral', '#F08080'), ('khaki', '#F0E68C'), ('aliceblue', '#F0F8FF'), ('honeydew', '#F0FFF0'), ('azure', '#F0FFFF'), ('wheat', '#F5DEB3'), ('beige', '#F5F5DC'), ('whitesmoke', '#F5F5F5'), ('mintcream', '#F5FFFA'), ('ghostwhite', '#F8F8FF'), ('salmon', '#FA8072'), ('sandybrown', '#FAA460'), ('antiquewhite', '#FAEBD7'), ('linen', '#FAF0E6'), ('lightgoldenrodyellow', '#FAFAD2'), ('oldlace', '#FDF5E6'), ('red', '#FF0000'), ('fuchsia', '#FF00FF'), ('magenta', '#FF00FF'), ('deeppink', '#FF1493'), ('orangered', '#FF4500'), ('tomato', '#FF6347'), ('hotpink', '#FF69B4'), ('coral', '#FF7F50'), ('darkorange', '#FF8C00'), ('lightsalmon', '#FFA07A'), ('orange', '#FFA500'), ('lightpink', '#FFB6C1'), ('pink', '#FFC0CB'), ('gold', '#FFD700'), ('peachpuff', '#FFDAB9'), ('navajowhite', '#FFDEAD'), ('moccasin', '#FFE4B5'), ('bisque', '#FFE4C4'), ('mistyrose', '#FFE4E1'), ('blanchedalmond', '#FFEBCD'), ('papayawhip', '#FFEFD5'), ('lavenderblush', '#FFF0F5'), ('seashell', '#FFF5EE'), ('cornsilk', '#FFF8DC'), ('lemonchiffon', '#FFFACD'), ('floralwhite', '#FFFAF0'), ('snow', '#FFFAFA'), ('yellow', '#FFFF00'), ('lightyellow', '#FFFFE0'), ('ivory', '#FFFFF0'), ('white', '#FFFFFF')) tv = tk.Canvas(win, width = win.winfo_width(), height = win.winfo_height()) tv.pack(side = "top") i = 0 rows = 35 w, h = 40, 10 w1, h1, h2 = 100, 28, 22 for ct in colorTuple: j = (i//rows) * 300 k = i * h2 - (i//rows) * h2 * rows x,y = j + w, k + h coord = x, y, j + w1, k + h1 tv.create_rectangle(coord,fill=ct[1]) tv.create_text((x+70,y+9),text=ct[1],anchor=tk.W,font=("宋體",9)) tv.create_text((x+120,y+9),text=ct[0],anchor=tk.W,font=("宋體",9)) i+=1 tv.update() for i in colorTuple: print(i[0],'\t' if len(i[0])>14 else '\t\t' if len(i[0])>6 else '\t\t\t',i[1])
運行效果圖:
這些顏色已經足夠多瞭,但據說裝瞭seaborn擴展的話xkcd_rgb包含的顏色數(有英文名稱的)會有更多……
實例三:圍棋盤
import tkinter as tk import pyautogui as ag win = tk.Tk() X,Y=ag.size() W,H=800,800 winPos = str(W)+"x"+str(H)+"+" winPos += str((X-W)//2)+"+" winPos += str((Y-H)//2) win.geometry(winPos) win.resizable(False, False) win.title('桌面分辨率:'+str(X)+"x"+str(Y)+' '*6+'窗口大小:'+str(W)+"x"+str(H)) win.update() tv = tk.Canvas(win, width = win.winfo_width(), height = win.winfo_height()) tv.pack(side = "top") for i in range(18): coord = 40, 40, 760, i*40 + 80 tv.create_rectangle(coord) coord = 40, 40, i*40 + 80, 760 tv.create_rectangle(coord) coord = 40, 40, 760, 760 tv.create_rectangle(coord,width=2) x = (160,160,160,400,400,400,640,640,640) y = (160,400,640,160,400,640,160,400,640) for i in range(9): tv.create_oval(x[i]-2,y[i]-2,x[i]+2,y[i]+2,fill='black') tv.update()
運行效果圖:
實例一中用到的 pyautogui,相關文章請見:
《python編程PyAutoGUI庫使用與安裝簡介》
以上就是Python編程tkinter庫Canvas實現塗鴉顏色表及圍棋盤示例的詳細內容,更多關於Python編程tkinter庫Canvas實現塗鴉顏色表圍棋盤的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- Python實現生命遊戲的示例代碼(tkinter版)
- python使用tkinter實現透明窗體
- 利用Python實現一個簡易的截圖工具
- tkinter使用js的canvas實現漸變色
- Tkinter canvas的畫佈參數,刪除組件,添加垂直滾動條詳解