Python tkinter写个画图板,一个坑和实用知识解析

用Python tkinter写个画图板很简单,本文也主要不是啰嗦的介绍画图板的代码写法,重点是想分享一个各种课程都未讲到但却非常实用的知识点和规避一个遇到的坑。
在这里插入图片描述

一、 程序框架简介

这个程序相对简单,就不逐句展开代码,确实有需要的可以私信赠送。
程序总的框架如下:

from tkinter import *

class Myapp(Tk):
    def __init__(self):
        super().__init__()
        self.title('万师兄')
        self.geometry('450x300+100+100')
        self.createWidget()

    def createWidget(self)
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的画图程序的代码: ```python from tkinter import * class PaintApp: def __init__(self, master): self.master = master self.master.title("Paint App") self.master.geometry("500x500") self.color = "black" self.size = 5 self.drawWidgets() self.canvas.bind("<B1-Motion>", self.paint) def paint(self, event): x1, y1 = (event.x - self.size), (event.y - self.size) x2, y2 = (event.x + self.size), (event.y + self.size) self.canvas.create_oval(x1, y1, x2, y2, fill=self.color, outline=self.color) def setColor(self, newcolor): self.color = newcolor def setSize(self, newsize): self.size = newsize def drawWidgets(self): self.controls = Frame(self.master, padx=5, pady=5) Label(self.controls, text="Pen Size:").grid(row=0, column=0) self.sizeSlider = Scale(self.controls, from_=1, to=10, orient=HORIZONTAL, command=self.setSize) self.sizeSlider.set(self.size) self.sizeSlider.grid(row=0, column=1) Label(self.controls, text="Pen Color:").grid(row=1, column=0) self.colorButtons = [ Button(self.controls, bg=color, width=3, height=1, command=lambda c=color: self.setColor(c)) for color in ["black", "red", "green", "blue", "yellow"] ] for i, button in enumerate(self.colorButtons): button.grid(row=1, column=i+1) self.controls.pack(side=TOP, pady=5) self.canvas = Canvas(self.master, width=400, height=400, bg="white") self.canvas.pack() root = Tk() paint_app = PaintApp(root) root.mainloop() ``` 这个程序使用`tkinter`模块创建了一个简单的画图应用。你可以通过调整画笔大小和颜色来绘制图形。程序支持的颜色包括黑色、红色、绿色、蓝色和黄色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值