Python编程tkinter库Canvas实现涂鸦颜色表及围棋盘示例
程序员文章站
2022-03-10 09:54:12
目录tkinter库canvas操作三个实例tkinter库canvas操作三个实例实例一:涂鸦import tkinter as tkimport pyautogui as agfrom time...
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 gui库pyautogui 0.9.52的下载、安装和使用》
以上就是python编程tkinter库canvas实现涂鸦颜色表及围棋盘示例的详细内容,更多关于python编程tkinter库canvas实现涂鸦颜色表围棋盘的资料请关注其它相关文章!
上一篇: 详解Redis缓存穿透/击穿/雪崩原理及其解决方案
下一篇: Redis事件处理及源码剖析