原文: http://zetcode.com/tkinter/drawing/

在 Tkinter 教程的这一部分中,我们将进行一些绘制。 在Canvas小部件上完成了 Tkinter 的绘制。 Canvas是用于在 Tkinter 中进行图形处理的高级工具。

它可用于创建图表,自定义窗口小部件或创建游戏。

直线

线是简单的几何图元。 create_line()方法在Canvas上创建一个订单项。

lines.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. The example draws lines on the Canvas.
  5. Author: Jan Bodnar
  6. Last modified: April 2019
  7. Website: www.zetcode.com
  8. """
  9. from tkinter import Tk, Canvas, Frame, BOTH
  10. class Example(Frame):
  11. def __init__(self):
  12. super().__init__()
  13. self.initUI()
  14. def initUI(self):
  15. self.master.title("Lines")
  16. self.pack(fill=BOTH, expand=1)
  17. canvas = Canvas(self)
  18. canvas.create_line(15, 25, 200, 25)
  19. canvas.create_line(300, 35, 300, 200, dash=(4, 2))
  20. canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85)
  21. canvas.pack(fill=BOTH, expand=1)
  22. def main():
  23. root = Tk()
  24. ex = Example()
  25. root.geometry("400x250+300+300")
  26. root.mainloop()
  27. if __name__ == '__main__':
  28. main()

在代码示例中,我们绘制了简单的线条。

  1. canvas.create_line(15, 25, 200, 25)

create_line()方法的参数是直线起点和终点的 x 和 y 坐标。

  1. canvas.create_line(300, 35, 300, 200, dash=(4, 2))

画一条垂直线。 dash选项指定线条的虚线图案。 我们有一条线,由 4px 虚线和 2px 间隔的交替部分组成。

  1. canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85)

create_line()方法可以取多个点。 这条线画了一个三角形。

Tkinter 中的绘图 - 图1

图:直线

颜色

颜色是代表红色,绿色和蓝色(RGB)强度值的组合的对象。

colours.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. This program draws three
  5. rectangles filled with different
  6. colours.
  7. Author: Jan Bodnar
  8. Last modified: April 2019
  9. Website: www.zetcode.com
  10. """
  11. from tkinter import Tk, Canvas, Frame, BOTH
  12. class Example(Frame):
  13. def __init__(self):
  14. super().__init__()
  15. self.initUI()
  16. def initUI(self):
  17. self.master.title("Colours")
  18. self.pack(fill=BOTH, expand=1)
  19. canvas = Canvas(self)
  20. canvas.create_rectangle(30, 10, 120, 80,
  21. outline="#fb0", fill="#fb0")
  22. canvas.create_rectangle(150, 10, 240, 80,
  23. outline="#f50", fill="#f50")
  24. canvas.create_rectangle(270, 10, 370, 80,
  25. outline="#05f", fill="#05f")
  26. canvas.pack(fill=BOTH, expand=1)
  27. def main():
  28. root = Tk()
  29. ex = Example()
  30. root.geometry("400x100+300+300")
  31. root.mainloop()
  32. if __name__ == '__main__':
  33. main()

在代码示例中,我们绘制了三个矩形,并用不同的颜色值填充了它们。

  1. canvas = Canvas(self)

我们创建Canvas小部件。

  1. canvas.create_rectangle(30, 10, 120, 80,
  2. outline="#fb0", fill="#fb0")

create_rectangle()在画布上创建一个矩形项目。 前四个参数是两个边界点的 x 和 y 坐标:左上角点和右下角点。 使用outline参数,我们可以控制矩形轮廓的颜色。 同样,fill参数为矩形的内部提供颜色。

Tkinter 中的绘图 - 图2

图:颜色

形状

我们可以在Canvas上绘制各种形状。 以下代码示例将显示其中的一些。

shapes.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. In this script, we draw basic
  5. shapes on the canvas.
  6. Author: Jan Bodnar
  7. Last modified: April 2019
  8. Website: www.zetcode.com
  9. """
  10. from tkinter import Tk, Canvas, Frame, BOTH
  11. class Example(Frame):
  12. def __init__(self):
  13. super().__init__()
  14. self.initUI()
  15. def initUI(self):
  16. self.master.title("Shapes")
  17. self.pack(fill=BOTH, expand=1)
  18. canvas = Canvas(self)
  19. canvas.create_oval(10, 10, 80, 80, outline="#f11",
  20. fill="#1f1", width=2)
  21. canvas.create_oval(110, 10, 210, 80, outline="#f11",
  22. fill="#1f1", width=2)
  23. canvas.create_rectangle(230, 10, 290, 60,
  24. outline="#f11", fill="#1f1", width=2)
  25. canvas.create_arc(30, 200, 90, 100, start=0,
  26. extent=210, outline="#f11", fill="#1f1", width=2)
  27. points = [150, 100, 200, 120, 240, 180, 210,
  28. 200, 150, 150, 100, 200]
  29. canvas.create_polygon(points, outline='#f11',
  30. fill='#1f1', width=2)
  31. canvas.pack(fill=BOTH, expand=1)
  32. def main():
  33. root = Tk()
  34. ex = Example()
  35. root.geometry("330x220+300+300")
  36. root.mainloop()
  37. if __name__ == '__main__':
  38. main()

我们在窗口上绘制五个不同的形状:一个圆形,一个椭圆形,一个矩形,一个弧形和一个多边形。 轮廓以红色绘制,内部以绿色绘制。 轮廓的宽度为 2 像素。

  1. canvas.create_oval(10, 10, 80, 80, outline="#f11",
  2. fill="#1f1", width=2)

此处create_oval()方法用于创建圈子项目。 前四个参数是圆的边界框坐标。 换句话说,它们是在其中绘制圆的框的左上和右下点的 x 和 y 坐标。

  1. canvas.create_rectangle(230, 10, 290, 60,
  2. outline="#f11", fill="#1f1", width=2)

我们创建一个矩形项目。 坐标还是要绘制的矩形的边界框。

  1. canvas.create_arc(30, 200, 90, 100, start=0,
  2. extent=210, outline="#f11", fill="#1f1", width=2)

该代码行创建了一条弧。 圆弧是圆的圆周的一部分。 我们提供边界框。 start参数是圆弧的起始角度。 extent是角度大小。

  1. points = [150, 100, 200, 120, 240, 180, 210,
  2. 200, 150, 150, 100, 200]
  3. canvas.create_polygon(points, outline='#f11',
  4. fill='#1f1', width=2)

创建一个多边形。 它是具有多个角的形状。 要在 Tkinter 中创建多边形,我们向create_polygon()方法提供了多边形坐标列表。

Tkinter 中的绘图 - 图3

图:形状

绘制图像

在下面的示例中,我们在画布上绘制一个图像项。

draw_image.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. In this script, we draw an image
  5. on the canvas.
  6. Author: Jan Bodnar
  7. Last modified: April 2019
  8. Website: www.zetcode.com
  9. """
  10. from tkinter import Tk, Canvas, Frame, BOTH, NW
  11. from PIL import Image, ImageTk
  12. class Example(Frame):
  13. def __init__(self):
  14. super().__init__()
  15. self.initUI()
  16. def initUI(self):
  17. self.master.title("High Tatras")
  18. self.pack(fill=BOTH, expand=1)
  19. self.img = Image.open("tatras.jpg")
  20. self.tatras = ImageTk.PhotoImage(self.img)
  21. canvas = Canvas(self, width=self.img.size[0]+20,
  22. height=self.img.size[1]+20)
  23. canvas.create_image(10, 10, anchor=NW, image=self.tatras)
  24. canvas.pack(fill=BOTH, expand=1)
  25. def main():
  26. root = Tk()
  27. ex = Example()
  28. root.mainloop()
  29. if __name__ == '__main__':
  30. main()

该示例在画布上显示图像。

  1. from PIL import Image, ImageTk

从 PIL(Python 图像库)模块,导入ImageImageTk模块。

  1. self.img = Image.open("tatras.jpg")
  2. self.tatras = ImageTk.PhotoImage(self.img)

Tkinter 在内部不支持 JPG 图像。 解决方法是,使用ImageImageTk模块。

  1. canvas = Canvas(self, width=self.img.size[0]+20,
  2. height=self.img.size[1]+20)

我们创建Canvas小部件。 它考虑了图像的大小。 它比实际图像大小宽 20px,高 20px。

  1. canvas.create_image(10, 10, anchor=NW, image=self.tatras)

我们使用create_image()方法在画布上创建一个图像项。 为了显示整个图像,它固定在北部和西部。 image参数提供要显示的照片图像。

绘制文字

在最后一个示例中,我们将在窗口上绘制文本。

draw_text.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. In this script, we draw text
  5. on the window.
  6. Author: Jan Bodnar
  7. Last modified: April 2019
  8. Website: www.zetcode.com
  9. """
  10. from tkinter import Tk, Canvas, Frame, BOTH, W
  11. class Example(Frame):
  12. def __init__(self):
  13. super().__init__()
  14. self.initUI()
  15. def initUI(self):
  16. self.master.title("Lyrics")
  17. self.pack(fill=BOTH, expand=1)
  18. canvas = Canvas(self)
  19. canvas.create_text(20, 30, anchor=W, font="Purisa",
  20. text="Most relationships seem so transitory")
  21. canvas.create_text(20, 60, anchor=W, font="Purisa",
  22. text="They're good but not the permanent one")
  23. canvas.create_text(20, 130, anchor=W, font="Purisa",
  24. text="Who doesn't long for someone to hold")
  25. canvas.create_text(20, 160, anchor=W, font="Purisa",
  26. text="Who knows how to love without being told")
  27. canvas.create_text(20, 190, anchor=W, font="Purisa",
  28. text="Somebody tell me why I'm on my own")
  29. canvas.create_text(20, 220, anchor=W, font="Purisa",
  30. text="If there's a soulmate for everyone")
  31. canvas.pack(fill=BOTH, expand=1)
  32. def main():
  33. root = Tk()
  34. ex = Example()
  35. root.geometry("420x250+300+300")
  36. root.mainloop()
  37. if __name__ == '__main__':
  38. main()

我们在窗口上画一首歌的歌词。

  1. canvas.create_text(20, 30, anchor=W, font="Purisa",
  2. text="Most relationships seem so transitory")

前两个参数是文本中心点的 x 和 y 坐标。 如果我们将文本项锚定在西方,则文本将从该位置开始。 font参数提供文本的字体,text参数是要显示的文本。

Tkinter 中的绘图 - 图4

图:绘制文本

在 Tkinter 教程的这一部分中,我们做了一些绘图。