gpt4 book ai didi

python - 如何在 tkinter Canvas 上绘制弧线?

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:15 25 4
gpt4 key购买 nike

我正在研究自动机理论,我被要求编写一个看起来或多或少像这样的自动机的图(树):

this

到目前为止我得到了这个(我正在使用 tkintercanvas 来绘制):

from tkinter import Tk, Canvas, mainloop

def circle(canvas, x, y, r, width):
id = canvas.create_oval (x-r, y-r, x+r, y+r, width = width)
return id

def line (canvas, x1, y1, x2, y2, width):
canvas.create_line (x1, y1, x2, y2, width = width)

def text (canvas, x, y, text):
canvas.create_text (x, y, text = text, font = ("bold", 20))

w = Canvas(Tk (), width=1000, height=600, bg = "white")

circle (w , 150, 300, 70, 3)
circle (w , 150, 300, 50, 3)
circle (w , 370, 300, 70, 3)
circle (w , 640, 300, 70, 3)
circle (w , 910, 300, 70, 3)

line (w, 10, 300, 80, 300, 3)
circle (w, 73, 300, 5, 6)
line (w, 220, 300, 300, 300, 3)
circle (w, 293, 300, 5, 6)
line (w, 440, 300, 570, 300, 3)
circle (w, 567, 300, 5, 6)
line (w, 710, 300, 840, 300, 3)
circle (w, 837, 300, 5, 6)

text (w, 150, 300, "q0")
text (w, 370, 300, "q1")
text (w, 640, 300, "q2")
text (w, 910, 300, "q3")

w.pack()
mainloop()

显示这个:

screenshot of image created by current code

我不需要箭头,因为我会用点代替。问题是我需要从圆 q3 到圆 q0 以及从圆 q0 到圆 q0 画一条线(“bucle”)。我尝试了 canvas.create_arc() 方法,但我无法获取它的句柄。有其他选择吗?关于如何绘制“bucle”的任何想法?

最佳答案

这里有一些实用函数,它们提供了在 tkinter.Canvas 上绘制弧线的替代方法。弧函数接受开始和停止 < em>角度 在 [0..360) 度的开放范围内。

它还说明了如何在直线(但不是圆弧)的末端绘制箭头,因为你也问过这个问题。

from tkinter import Canvas, mainloop, Tk

def circle(canvas, x, y, r, width):
return canvas.create_oval(x+r, y+r, x-r, y-r, width=width)

def circular_arc(canvas, x, y, r, t0, t1, width):
return canvas.create_arc(x-r, y-r, x+r, y+r, start=t0, extent=t1-t0,
style='arc', width=width)

def ellipse(canvas, x, y, r1, r2, width):
return canvas.create_oval(x+r1, y+r2, x-r1, y-r2, width=width)

def elliptical_arc(canvas, x, y, r1, r2, t0, t1, width):
return canvas.create_arc(x-r1, y-r2, x+r1, y+r2, start=t0, extent=t1-t0,
style='arc', width=width)

def line(canvas, x1, y1, x2, y2, width, start_arrow=0, end_arrow=0):
arrow_opts = start_arrow << 1 | end_arrow
arrows = {0b10: 'first', 0b01: 'last', 0b11: 'both'}.get(arrow_opts, None)
return canvas.create_line(x1, y1, x2, y2, width=width, arrow=arrows)

def text(canvas, x, y, text):
return canvas.create_text(x, y, text=text, font=('bold', 20))


w = Canvas(Tk(), width=1000, height=600, bg='white')

circle(w, 150, 300, 70, 3) # q0 outer edge
circle(w, 150, 300, 50, 3) # q0 inner edge
circle(w, 370, 300, 70, 3) # q1
circle(w, 640, 300, 70, 3) # q2
circle(w, 910, 300, 70, 3) # q3

# Draw arc from circle q3 to q0.
midx, midy = (150+910) / 2, 300
r1, r2 = 910-midx, 70+70
elliptical_arc(w, midx, midy, r1, r2, 30, 180-30, 3)

line(w, 10, 300, 80, 300, 3, end_arrow=1)
line(w, 220, 300, 300, 300, 3, end_arrow=1)
line(w, 440, 300, 570, 300, 3, end_arrow=1)
line(w, 710, 300, 840, 300, 3, end_arrow=1)

text(w, 150, 300, 'q0')
text(w, 370, 300, 'q1')
text(w, 640, 300, 'q2')
text(w, 910, 300, 'q3')

w.pack()
mainloop()

这是它画的:

screenshot showing output

它没有像您想要的那样绘制“带扣”,部分原因是绘制“从圆 q3 到圆 q0,以及从圆 q0 到圆 q0 的线”不像问题开头的插图那样一个画在两个圆圈之间(如果我理解正确的话)。

但是,它确实为您提供了另一种在canvas 上绘制圆弧的方法。

关于python - 如何在 tkinter Canvas 上绘制弧线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46146615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com