鱼C论坛

 找回密码
 立即注册
查看: 2914|回复: 2

[作品展示] 绘图软件

[复制链接]
发表于 2020-11-14 13:28:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 张育玮 于 2020-11-14 13:30 编辑
直接上代码^  -  ^
  1. from tkinter import *
  2. from tkinter.colorchooser import *

  3. root = Tk()
  4. root.configure(background="gray")

  5. instruction = Label(root,text="用鼠标左键在画布上画画吧",background= "gray")
  6. instruction.pack() # 1

  7. colorButton = Button(root,text="选择颜色")
  8. colorButton.pack() # 2

  9. rectButten = Button(root,text="方形")
  10. rectButten.pack() # 3

  11. lineButton = Button(root,text="线条")
  12. lineButton.pack() # 4

  13. circleButton = Button(root,text="圆圈")
  14. circleButton.pack() # 5

  15. clearButton = Button(root,text="清除")
  16. clearButton.pack() # 6

  17. myCanvas = Canvas(root,width=400,height=300)
  18. myCanvas.pack() # 7

  19. myShape = "line" #使用myShape变量存储当前绘画的图形
  20. myColor = "black"

  21. def pen_down(event):
  22.     global prevX
  23.     global prevY
  24.     prevX = event.x
  25.     prevY = event.y
  26. myCanvas.bind("<ButtonPress-1>",pen_down)
  27. # 按下鼠标左键时,得到X、Y的坐标

  28. def draw (event):
  29.     global prevX
  30.     global prevY
  31.     if myShape == "line":
  32.         myCanvas.create_line(prevX,prevY,event.x,event.y,fill=myColor)
  33.         prevX = event.x
  34.         prevY = event.y
  35. myCanvas.bind("<B1-Motion>",draw)#当移动鼠标时,如果选择画线,就画线。

  36. def pen_up(event):
  37.     if myShape == "circle":
  38.         myCanvas.create_oval(prevX,prevY,event.x,event.y,fill=myColor)
  39.     if myShape == "rectangle":
  40.         myCanvas.create_rectangle(prevX,prevY,event.x,event.y,fill=myColor)
  41. myCanvas.bind("<ButtonRelease-1>",pen_up)
  42. #松开鼠标左键时,如果myShape里是圆形,就绘制圆形,松开鼠标左键时,如果myShape里是矩形,就绘制炬形。

  43. def pick_color(event):
  44.     global myColor
  45.     myColor = askcolor()
  46.     myColor = myColor[1]
  47. colorButton.bind("<Button-1>",pick_color)

  48. def clear(event):
  49.     myCanvas.delete(ALL)
  50. clearButton.bind("<Button-1>", clear)

  51. def set_line(event):
  52.     global myShape
  53.     myShape = "line"
  54. lineButton.bind("<Button-1>",set_line)
  55. #点击线条按钮时,设myShape为线条“line”

  56. def set_rect(event):
  57.     global myShape
  58.     myShape = "rectangle"
  59. rectButten.bind("<Button-1>",set_rect)
  60. #点击方形按钮,设myShape为矩形。
  61. def set_circle(event):
  62.     global myShape
  63.     myShape = "circle"
  64. circleButton.bind("<Button-1>",set_circle)

  65. mainloop()
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-14 15:49:38 | 显示全部楼层
这不错啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-15 11:16:02 | 显示全部楼层

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 15:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表