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()


目录
相关文章
|
数据采集 监控 数据管理
数据治理之道:大数据平台的搭建与数据质量管理
【10月更文挑战第26天】随着信息技术的发展,数据成为企业核心资源。本文探讨大数据平台的搭建与数据质量管理,包括选择合适架构、数据处理与分析能力、数据质量标准与监控机制、数据清洗与校验及元数据管理,为企业数据治理提供参考。
525 1
|
10月前
|
人工智能 机器人 测试技术
从支撑英伟达GR00T到登陆魔搭社区,智元AgiBot World打通具身智能全球数据生态
备受关注的 AgiBot World 百万真机数据集正式登陆国内顶级 AI开源社区——魔搭社区。该数据集由智元机器人开发,此前已在GitHub 和 Hugging Face 等国际平台开源并获得了业界的积极反响。此举是智元机器人布局国内AI生态的重要一步,国内开发者和研究者将能够更加顺畅地接入AgiBot World全套资源,降低数据获取和工具使用门槛,推动具身智能及机器人技术在国内的普及与发展。
372 0
|
数据采集 缓存 监控
如何提高爬虫的抓取效率
提高爬虫的抓取效率是爬虫开发中的一个重要目标。以下是一些可以提高爬虫抓取效率的方法和技巧: 1. 合理设置请求频率 避免过高频率:频繁的请求可能会对目标服务器造成过大压力,甚至导致被封禁。合理设置请求间隔时间,例如每次请求间隔几秒到几十秒。 动态调整频率:根据目标网站的响应时间动态调整请求频率。如果响应时间较长,适当降低请求频率;如果响应时间较短,可以适当提高请求频率。
466 6
|
人工智能 安全 搜索推荐
阿里云采购季:短信服务低至 0.01 元/条!
阿里云“上云采购季”,短信服务低至 0.01 元/条
553 3
|
人工智能 Java 程序员
通义灵码AI编码助手和AI程序员背后的技术
通义灵码AI编码助手和AI程序员背后的技术,由通义实验室科学家黎槟华分享。内容涵盖三部分:1. 编码助手技术,包括构建优秀AI编码助手及代码生成补全;2. 相关的AI程序员技术,探讨AI程序员的优势、发展情况、评估方法及核心难点;3. 代码智能方向的展望,分析AI在软件开发中的角色转变,从辅助编程到成为开发主力,未来将由AI执行细节任务,开发者负责决策和审核,大幅提升开发效率。
692 12
|
机器学习/深度学习 编解码 算法
在线打开CAD或Solidworks的STP文件,通过以图搜图与实物比对搜索
智能比对系统利用大模型技术,实现设计图纸与实物的高效、精准比对。系统支持在线3D模型解析、多视图图片自动生成、实物照片智能比对及实时偏差标注,全面提升机械制造行业的设计、生产和质量控制效率。
1019 2
|
JSON 测试技术 API
Python开发解析Swagger文档小工具
文章介绍了如何使用Python开发一个解析Swagger文档的小工具,该工具可以生成符合httprunner测试框架的json/yaml测试用例,同时还能输出Excel文件,以方便测试人员根据不同需求使用。文章提供了详细的开发步骤、环境配置和使用示例,并鼓励读者为该开源项目贡献代码和建议。
830 1
Python开发解析Swagger文档小工具
|
机器学习/深度学习 人工智能 自然语言处理
【人工智能】Transformers之Pipeline(三):文本转音频(text-to-audio/text-to-speech)
【人工智能】Transformers之Pipeline(三):文本转音频(text-to-audio/text-to-speech)
661 1
【人工智能】Transformers之Pipeline(三):文本转音频(text-to-audio/text-to-speech)
阿里云国际版账号登录不上去,账号被风控怎么办?
阿里云国际版账号登录不上去,账号被风控怎么办?
|
网络协议 安全 网络安全
什么是 IP 欺骗?
【8月更文挑战第24天】
638 0