我正在学习自动机理论,需要编写一个类似下图所示的自动机图形(树形结构):
目前我已经得到了以下结果(使用tkinter
和canvas
进行绘制):
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()
这会显示出以下内容:
我不需要箭头,因为我要用点代替它们。问题是我需要从圆圈q3到圆圈q0画一条线,并且也需要从圆圈q0到圆圈q0画一条线(一个“bucle”)。我尝试了canvas.create_arc()
方法,但我无法掌握它。还有其他方法吗?有没有关于如何绘制“bucle”的想法?
canvas.create_arc()
方法有什么不理解的地方呢?请具体说明或者展示你尝试使用它的代码。顺便提一句,canvas.create_line()
方法支持箭头。 - martineaucanvas.create_arc()
和canvas.create_line()
方法的更新文档链接。 - martineau