在本文中,我们将在 Python Tkinter GUI 中设计和构建一个基本的绘图应用程序,在这里我们可以简单地使用铅笔在画布上绘制一些东西并用橡皮擦擦除它,以及改变铅笔粗细的和橡皮擦。我们还可以修改画布的背景颜色并将特定的绘图保存在我们的本地计算机上。

from tkinter import *
from tkinter.ttk import Scale
from tkinter import colorchooser, filedialog, messagebox
import PIL.ImageGrab as ImageGrab
# from ttkbootstrap import ttk
from tkmacosx import Button# Defining Class and constructor of the Program
class Draw():def __init__(self, root):# Defining title and Size of the Tkinter Window GUIself.root = rootself.root.title("画图工具Python")self.root.geometry("810x566")self.root.configure(background="white")#         self.root.resizable(0,0)# variables for pointer and Eraserself.pointer = "black"self.erase = "white"# Widgets for Tkinter Window# Configure the alignment , font size and color of the texttext = Text(root, height=2, width=100)text.tag_configure("tag_name", justify='center', font=('arial', 25), background='#292826', foreground='orange')# Insert a Texttext.insert("1.0", "python中画画")# Add the tag for following given texttext.tag_add("tag_name", "1.0", "end")text.pack()# Pick a color for drawing from color pannelself.pick_color = LabelFrame(self.root, text='Colors', font=('arial', 15), bd=5, relief=RIDGE, bg="white")self.pick_color.place(x=0, y=40, width=135, height=525)colors = ['blue', 'red', 'green', 'orange', 'violet', 'black', 'yellow', 'purple', 'pink', 'gold', 'brown','indigo']i = j = 0for color in colors:Button(self.pick_color, bg=color, width=61,command=lambda col=color: self.select_color(col)).grid(row=i, column=j)i += 1if i == 6:i = 0j = 1# 擦除按钮self.eraser_btn = Button(self.root, text="Eraser", command=self.eraser, width=120)self.eraser_btn.place(x=7, y=230)  # 改# 清屏self.clear_screen = Button(self.root, text="Clear Screen",width=120,command=lambda: self.background.delete('all'))self.clear_screen.place(x=7, y=260)# 保存self.save_btn = Button(self.root, text="ScreenShot",command=self.save_drawing,width=120)self.save_btn.place(x=7, y=290)# 改变画布背景self.bg_btn = Button(self.root, text="Background", command=self.canvas_color,width=120)self.bg_btn.place(x=7, y=320)# 改变画笔大小self.pointer_frame = LabelFrame(self.root, text='size', bd=5, bg='white', font=('arial', 15, 'bold'),relief=RIDGE)self.pointer_frame.place(x=33, y=360, height=200, width=70)self.pointer_size = Scale(self.pointer_frame, orient=VERTICAL, from_=1, to=48, length=168)self.pointer_size.set(1)self.pointer_size.grid(row=0, column=1, padx=15)# 定一个画布self.background = Canvas(self.root, bg='white', bd=5, relief=GROOVE, height=507, width=650)self.background.place(x=140, y=45)# 监听画布被点击self.background.bind("<B1-Motion>", self.paint)# Functions are defined here# 画笔函数def paint(self, event):x1, y1 = (event.x - 2), (event.y - 2)x2, y2 = (event.x + 2), (event.y + 2)self.background.create_oval(x1, y1, x2, y2, fill=self.pointer, outline=self.pointer,width=self.pointer_size.get())# Function for choosing the color of pointerdef select_color(self, col):self.pointer = col# Function for defining the eraserdef eraser(self):self.pointer = self.erase# Function for choosing the background color of the Canvasdef canvas_color(self):color = colorchooser.askcolor()self.background.configure(background=color[1])self.erase = color[1]# Function for saving the image file in Local Computerdef save_drawing(self):try:# self.background update()file_ss = filedialog.asksaveasfilename(defaultextension='jpg')# print(file_ss)x = self.root.winfo_rootx() + self.background.winfo_x()# print(x, self.background.winfo_x())y = self.root.winfo_rooty() + self.background.winfo_y()# print(y)x1 = x + self.background.winfo_width()# print(x1)y1 = y + self.background.winfo_height()# print(y1)ImageGrab.grab().crop((x, y, x1, y1)).save(file_ss)messagebox.showinfo('Screenshot Successfully Saved as' + str(file_ss))except:print("Error in saving the screenshot")if __name__ == "__main__":root = Tk()p = Draw(root)root.mainloop()


注意:
目前Mac测试OK。 windows需要改样式或者一些参数 具体自行研究
另外

Python+Tkinter画图工具相关推荐

  1. python实现画图工具

    更多编程教程请到:菜鸟教程 https://www.piaodoo.com/ 友情链接:好看站 http://www.nrso.net/ 高州阳光论坛https://www.hnthzk.com/ 简 ...

  2. python画图程序有图-Python海龟画图工具绘制叮当猫程序

    使用海龟画图工具,画一个叮当猫. 先学习几个基本函数: import turtle;#引入海龟工具模块 t=turtle.Pen()#创建海龟画笔 t.fillcolor("blue&quo ...

  3. python+windows画图工具--复现别人论文中的colormap 方法2

    如何复现别人论文中的colormap2 首先,将别人论文中的colormap截图之后,拖到windows自带的画图工具中,选择吸管工具,然后点击其中一个颜色,(这里以图中的蓝色为例),再点击吸管工具, ...

  4. 用python画猫咪怎么画-Python海龟画图工具绘制叮当猫程序

    t.fillcolor("blue")#填充颜色 t.begin_fill()#开始填充 t.circle(160)#画圆 t.end_fill()#结束填充 t.up() #鼠标 ...

  5. python pylab画图工具

    pylab 是 matplotlib 面向对象绘图库的一个接口,它的语法和 Matlab 十分相近,主要的绘图命令和 Matlab 对应的命令有相似的参数 form pylab import *# 普 ...

  6. python脚本绘图_python实现画图工具

    简易画图工具(Python),供大家参考,具体内容如下 小黑最近在努力的入门python,正好学习到了Python的tkinker模块下的Canvas(画布)和Button(按钮)再加上相应的事务管理 ...

  7. 简易画图工具(Python)

    简易画图工具(Python) 小黑最近在努力的入门python,正好学习到了Python的tkinker模块下的Canvas(画布)和Button(按钮)再加上相应的事务管理,实现了一个简单的画图小工 ...

  8. Python超简单容易上手的画图工具库

    今天,在网上发现一款很棒的python画图工具库.很简单的api调用就能生成漂亮的图表.并且可以进行一些互动. pyecharts 是一个用于生成 Echarts 图表的类库.Echarts 是百度开 ...

  9. matplotlib画图使用python可视化colorbar工具自定义颜色

    python matplotlib画图使用colorbar工具自定义颜色 colorbar(draw colorbar without any mapple/plot) 自定义colorbar可以画出 ...

最新文章

  1. 屡现黑马!2021THE泰晤士高等教育学科排名发布!斯坦福成为最大赢家,清华、北大强势逆袭!...
  2. AJAX技术其实就是多年前我就使用过的XMLHTTP
  3. 如何在Ubuntu中安装java jdk
  4. python实现抓取必应图片设置桌面
  5. 用python读取股票价格_使用Python写一个量化股票提醒系统
  6. 【学术相关】一院士给博士生的一封信:每天工作12小时,这仅是一个下限!...
  7. java .this的用法_JAVA中this用法小结
  8. 《Python Cookbook 3rd》笔记(2.4):字符串匹配和搜索
  9. Qt4_IconEditor窗口部件
  10. 会c 学plc编程语言,想学PLC编程?先弄清5种PLC专用语言!
  11. MySQL 如何查看表的存储引擎
  12. sqlserver按'一二三四五'排序(hibernate对中文数字进行排序)
  13. 【Android自定义控件】仿京东首页的京东快报,自动向上滚动的广告条
  14. MATLAB图像检索系统GUI设计
  15. 利用中文维基百科训练词向量模型
  16. 两个数的最大公因数和最小公倍数
  17. 平面设计初学者如何入门
  18. 【★】生成树算法终极解析!
  19. 阴暗、恐怖、外星人?这是一款什么样的游戏?
  20. 产品狗的Python之路(2):excel表格拆分桌面小程序

热门文章

  1. Spring Integration
  2. Python 带你走进哈利波特的魔法世界
  3. 三维动画制作流程细节
  4. dtree.js菜单列表加密,实现Code
  5. 欧盟站POPs持久性有机污染物测试报告
  6. 2020-01-24
  7. al11-添加SAP目录
  8. Kyan 网络监控设备 hosts 账号密码泄露漏洞
  9. 关于webpack下载loader时遇到的问题
  10. 测试5g网速的软件排行榜,5G网速测试