如何在Tkinter上绘制看起来平滑的线条

huangapple go评论80阅读模式
英文:

How to draw smooth looking lines on Tkinter

问题

以下是代码部分的翻译:

import tkinter as tk

class DrawingApp:
    def __init__(self, master):
        self.master = master
        self.canvas = tk.Canvas(self.master, width=1024, height=256)
        self.canvas.pack()

        self.previous_coordinates = None
        self.canvas.bind("<B1-Motion>", self.draw)
        self.canvas.bind("<Button-3>", self.erase)
        self.canvas.bind("<Button-1>", self.draw)
        self.canvas.bind("<ButtonRelease-1>", self.reset)

    def draw(self, event):
        if self.previous_coordinates:
            x1, y1 = self.previous_coordinates
            x2, y2 = event.x, event.y
            self.canvas.create_line(x1, y1, x2, y2, fill="black", width=20, capstyle=tk.PROJECTING,
                                    joinstyle=tk.BEVEL, smooth=True, splinesteps=36)
        self.previous_coordinates = (event.x, event.y)

    def erase(self, event):
        # 删除画布上的所有绘图
        self.canvas.delete("all")

    def reset(self, event):
        self.previous_coordinates = None

root = tk.Tk()
app = DrawingApp(root)
root.mainloop()

希望这有所帮助。如果您有其他问题,可以继续提问。

英文:

I want to make a simple drawing app using Tkinter, I want to draw lines using my mouse, the solution that I came up with works fine, and I can even draw multiple lines but in the picture below you can see that lines look jagged. My question is can I draw smooth looking lines on Tkinter?

如何在Tkinter上绘制看起来平滑的线条

Here is the code that I am using to draw lines on the canvas:

import tkinter as tk


class DrawingApp:
    def __init__(self, master):
        self.master = master
        self.canvas = tk.Canvas(self.master, width=1024, height=256)
        self.canvas.pack()

        self.previous_coordinates = None
        self.canvas.bind(&quot;&lt;B1-Motion&gt;&quot;, self.draw)
        self.canvas.bind(&quot;&lt;Button-3&gt;&quot;, self.erase)
        self.canvas.bind(&quot;&lt;Button-1&gt;&quot;, self.draw)
        self.canvas.bind(&quot;&lt;ButtonRelease-1&gt;&quot;, self.reset)

    def draw(self, event):
        if self.previous_coordinates:
            x1, y1 = self.previous_coordinates
            x2, y2 = event.x, event.y
            self.canvas.create_line(x1, y1, x2, y2, fill=&quot;black&quot;, width=20, capstyle=tk.PROJECTING,
                                    joinstyle=tk.BEVEL, smooth=True, splinesteps=36)
        self.previous_coordinates = (event.x, event.y)

    def erase(self, event):
        # delete every drawing in the canvas
        self.canvas.delete(&quot;all&quot;)

    def reset(self, event):
        self.previous_coordinates = None


root = tk.Tk()
app = DrawingApp(root)
root.mainloop()

答案1

得分: 0

尝试将capstyle选项更改为tk.ROUND,可能会提高平滑度。

还要注意,smoothsplinesteps选项对于两点之间的直线无效。

self.canvas.create_line(x1, y1, x2, y2, fill="black", width=20,
                        capstyle=tk.ROUND, joinstyle=tk.BEVEL)

结果:

如何在Tkinter上绘制看起来平滑的线条

英文:

Try changing capstyle option to tk.ROUND and it may improve the smoothness a bit.

Note also that smooth and splinesteps options are useless of a straight line between two points.

self.canvas.create_line(x1, y1, x2, y2, fill=&quot;black&quot;, width=20, 
                        capstyle=tk.ROUND, joinstyle=tk.BEVEL)

Result:

如何在Tkinter上绘制看起来平滑的线条

huangapple
  • 本文由 发表于 2023年4月11日 14:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定