00.tkinter教程-tkinter画图板实战

简介: 00.tkinter教程-tkinter画图板实战

配套视频教程地址

配套视频教程

from tkinter import *
from tkinter.colorchooser import askcolor
class Paint(object):
    DEFAULT_PEN_SIZE = 5.0
    DEFAULT_COLOR = 'black'
    def __init__(self):
        self.root = Tk()
        self.root.title("PaintPy")
        # ------------TOP UI------------
        self.menubar = Menu(self.root)
        self.fileMenu = Menu(self.root, tearoff=0)
        self.editMenu = Menu(self.root, tearoff=0)
        self.viewMenu = Menu(self.root, tearoff=0)
        # Creating the TOP file menu
        self.menubar.add_cascade(label="File", menu=self.fileMenu)
        # Dropdown Menu for file menu
        self.fileMenu.add_command(label="Open")
        self.fileMenu.add_command(label="Save")
        self.fileMenu.add_separator()
        self.fileMenu.add_command(label="Exit", command=self.quitApp)
        # Creating EDIT tab in menubar
        self.menubar.add_cascade(label="Edit", menu=self.editMenu)
        self.editMenu.add_command(label="Undo")
        self.editMenu.add_command(label="Clear Canvas", command=self.deleteAll)
        # Creating the View tab in the menubar, next to file
        self.menubar.add_cascade(label="View", menu=self.viewMenu)
        self.viewMenu.add_command(label="Fullscreen", command=self.fullscreen)
        self.viewMenu.add_command(label="Hide UI", command=self.hideUI)
        self.viewMenu.add_separator()
        self.viewMenu.add_command(label="About", command=self.aboutPage)
        # ------------main UI------------
        self.penButton = Button(self.root, text='pen', command=self.usePen)
        self.penButton.grid(row=0, column=0)
        self.shapeButton = Button(
            self.root, text='Shape', command=self.changeShape)
        self.shapeButton.grid(row=0, column=1)
        self.colorButton = Button(
            self.root, text='color', command=self.chooseColor)
        self.colorButton.grid(row=0, column=2)
        self.eraserButton = Button(
            self.root, text='eraser', command=self.useEraser)
        self.eraserButton.grid(row=0, column=3)
        self.sizeSlider = Scale(
            self.root, from_=1, to=10, orient=HORIZONTAL)
        self.sizeSlider.grid(row=0, column=4)
        # Sets resolution of the window, color of the canvas
        self.background = Canvas(self.root, bg='white', width=950, height=700,)
        self.background.grid(row=1, columnspan=5)
        # configures and dsiplays the menu bar (file/View)
        self.root.config(menu=self.menubar)
        self.setup()
        self.root.mainloop()
    def setup(self):
        self.oldX = None
        self.oldY = None
        self.lineWidth = self.sizeSlider.get()
        self.color = self.DEFAULT_COLOR
        self.eraserOn = False
        self.activeButton = self.penButton
        # Sets mouse click one to paint on the canvas
        # TODO if shape is selected, draw shape instead
        self.background.bind('<B1-Motion>', self.paint)
        # Stops the mouse from drawing on M1 release
        self.background.bind('<ButtonRelease-1>', self.reset)
    # Hides the top ui from the user
    def hideUI(self):
        self.penButton.grid_remove()
        self.shapeButton.grid_remove()
        self.colorButton.grid_remove()
        self.eraserButton.grid_remove()
        self.sizeSlider.grid_remove()
    # TODO implement this into menu bar
    def restoreUI(self):
        self.penButton.grid()
        self.shapeButton.grid()
        self.colorButton.grid()
        self.eraserButton.grid()
        self.sizeSlider.grid()
    def usePen(self):
        self.activateButton(self.penButton)
    def changeShape(self):
        self.activateButton(self.shapeButton)
        self.top = Toplevel()
        self.shapeMenu = Menu(self.top)
        self.top.title("Shape Selection")
        self.msg = Message(self.top, text="sample text")
        self.msg.pack()
    def aboutPage(self):
        self.about = Toplevel(master=None, padx=10, pady=10)
        self.about.title("About PaintPy")
        self.msg = Message(
            self.about, text="Created by Jonathan Szkup using Python")
        self.msg.pack()
    def chooseColor(self):
        self.eraserOn = False
        self.color = askcolor(color=self.color)[1]
    def useEraser(self):
        # Eraser sets color of the brush to white
        self.activateButton(self.eraserButton, eraserMode=True)
    def activateButton(self, someButton, eraserMode=False):
        # On button click the button will sink
        # if selectable will stay sunken
        self.activeButton.config(relief=RAISED)
        someButton.config(relief=SUNKEN)
        self.activeButton = someButton
        self.eraserOn = eraserMode
    def fullscreen(self):
        # TODO scale the whole drawing window to resolution (Canvas())
        self.root.attributes("-fullscreen", True)
        # menu.entryconfigure(1, label="Exit Fullscreen")
        # TODO if already in fullscreen have the text change to exit fullscreen, and leave fullscreen
    def paint(self, event):
        # Gets the size of the stroke
        self.lineWidth = self.sizeSlider.get()
        # Sets the eraser to be the color "white"
        paintColor = 'white' if self.eraserOn else self.color
        # Painting based on position of the mouse relative to the canvas
        if self.oldX and self.oldY:
            self.background.create_line(self.oldX, self.oldY, event.x, event.y,
                                        width=self.lineWidth, fill=paintColor,
                                        capstyle=ROUND, smooth=TRUE,
                                        splinesteps=36)
        self.oldX = event.x
        self.oldY = event.y
    # stops drawing
    def reset(self, event):
        self.oldX, self.oldY = None, None
    # Deletes everything on the canvas
    def deleteAll(self):
        self.background.delete("all")
    # Closes PaintPy
    def quitApp(self):
        self.root.quit()
if __name__ == '__main__':
    Paint()


目录
相关文章
|
运维 安全 Linux
宝塔面板搭建网站教程:Linux下使用宝塔一键搭建网站,内网穿透发布公网上线
宝塔面板搭建网站教程:Linux下使用宝塔一键搭建网站,内网穿透发布公网上线
1052 0
|
9月前
|
缓存 运维 安全
核心转储为什么必须限制?一次讲清 Core Dump 的原理、风险与安全处理步骤·卓伊凡
《卓伊凡·网络安全研究室》详解核心转储(Core Dump):程序崩溃时内存快照,可能泄露账号、密钥、Token等敏感信息。本文剖析其危险性,用“办公室晕倒全景照”形象比喻,并提供生产环境禁用、systemd/sysctl配置、权限隔离、审计清理等八步安全实践方案。(239字)
689 7
|
9月前
|
人工智能 JavaScript API
零基础也能会!阿里云轻量服务器一键部署OpenClaw(Clawdbot)步骤+常见问题解答汇总
OpenClaw(曾用名Clawdbot、Moltbot)是一款具备“思考+执行”能力的开源AI自动化代理,并非传统聊天机器人,而是能通过自然语言指令完成邮件管理、网页抓取、文档处理等实际任务的AI助手。其核心优势在于零代码自动化、插件化扩展(内置Clawhub技能市场,提供数百款现成插件)、多端交互(支持Telegram、钉钉等IM工具集成)以及与阿里云产品的深度适配,可无缝对接百炼大模型、OSS存储等服务,实现能力闭环。
478 2
|
8月前
|
传感器 人工智能 运维
BIM与数字孪生融合架构:智慧楼宇运维系统技术解析
本文以浦东世博滨江智慧楼宇改造为范例,阐述BIM与数字孪生融合架构在建筑运维数字化转型中的工程实践:通过867个传感器构建感知层,28万智能组件打造动态数字孪生体,零代码平台赋能非技术人员快速配置,AI引擎实现92%故障预测准确率,支撑能源优化、智能照明、安防预警等场景落地,推动楼宇向可感知、会思考、自进化的智慧生命体演进。(239字)
|
人工智能 机器人 测试技术
从支撑英伟达GR00T到登陆魔搭社区,智元AgiBot World打通具身智能全球数据生态
备受关注的 AgiBot World 百万真机数据集正式登陆国内顶级 AI开源社区——魔搭社区。该数据集由智元机器人开发,此前已在GitHub 和 Hugging Face 等国际平台开源并获得了业界的积极反响。此举是智元机器人布局国内AI生态的重要一步,国内开发者和研究者将能够更加顺畅地接入AgiBot World全套资源,降低数据获取和工具使用门槛,推动具身智能及机器人技术在国内的普及与发展。
649 0
|
分布式计算 Java Go
golang和java对比
golang和java对比
1167 0
|
人工智能 安全 搜索推荐
阿里云采购季:短信服务低至 0.01 元/条!
阿里云“上云采购季”,短信服务低至 0.01 元/条
814 3
|
并行计算 PyTorch 算法框架/工具
基于CUDA12.1+CUDNN8.9+PYTORCH2.3.1,实现自定义数据集训练
文章介绍了如何在CUDA 12.1、CUDNN 8.9和PyTorch 2.3.1环境下实现自定义数据集的训练,包括环境配置、预览结果和核心步骤,以及遇到问题的解决方法和参考链接。
1359 5
基于CUDA12.1+CUDNN8.9+PYTORCH2.3.1,实现自定义数据集训练
|
机器学习/深度学习 编解码 算法
在线打开CAD或Solidworks的STP文件,通过以图搜图与实物比对搜索
智能比对系统利用大模型技术,实现设计图纸与实物的高效、精准比对。系统支持在线3D模型解析、多视图图片自动生成、实物照片智能比对及实时偏差标注,全面提升机械制造行业的设计、生产和质量控制效率。
1290 2