简体   繁体   English

在 tkinter 中,有没有办法绘制平滑的曲线?

[英]In tkinter is there a way to draw a smoothed out curved line?

A line item can actually be a series of line segments, not just one;一个行项目实际上可以是一系列线段,而不仅仅是一个; in our example, we could have chosen to use a single line item for each complete stroke.在我们的示例中,我们可以选择为每个完整的笔划使用一个单行项目。 The line can also be drawn directly point-to-point, or smoothed out into a curved line.该线也可以直接点对点绘制,或平滑成曲线。

These lines are from the tkinter docs - https://tkdocs.com/tutorial/canvas.html#types这些行来自tkinter 文档- https://tkdocs.com/tutorial/canvas.html#types

But in this it says that a line(on the tkinter canvas) can be drawn directly point-to-point, or smoothed out into a curved line.但是在这里它说一条线(在 tkinter 画布上)可以直接点对点绘制,或者平滑成曲线。

But I have not yet been able to find a way to do that, kindly help me with the same.但是我还没有找到一种方法来做到这一点,请帮助我。

Edit:编辑:

My code is too big and messy for it to be completely shown but I hope these three functions below will help people.我的代码太大而且太乱,无法完整显示,但我希望下面的这三个函数对人们有所帮助。

Also it really just about a normal painting program(or atleast you can take it like one.)而且它实际上只是一个普通的绘画程序(或者至少你可以把它当作一个。)

def clear_prev_draw_coords(event = None) :
    global last_draw_x, last_draw_y, pen_color, pen_size

    last_draw_x, last_draw_y = None, None
    drawing_coord_list.clear()
    return

def draw() :
    last_img_x = 0
    last_img_y = 0
    canvas.create_line(last_draw_x, last_draw_y, x2, y2, fill = pen_color, width = pen_size, tag = tag, smooth = 1)

    last_draw_x, last_draw_y = x2, y2
    return

def toggle_pen_mode(dummy = None) :
    global mode

    mode = 'pen'

    root.bind("<B1-Motion>", draw)
    root.bind("<ButtonRelease-1>", clear_prev_draw_coords)
    return

Have you read effbot ?你读过effbot吗? You can find a lot of information about all possible flags and options.您可以找到有关所有可能的标志和选项的大量信息。 This mini example creates a curved line with the smooth -flag:这个迷你示例使用smooth -flag 创建一条曲线:

from tkinter import *

master = Tk()

w = Canvas(master, width=200, height=100)
w.pack()

w.create_line(150,0, 100,50, 50,0, 0,50, smooth=1)

mainloop()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM