关于python:如何在tkinter中绘制对角箭头?

How to draw diagonal arrows in tkinter?

我需要从蓝色和绿色的盒子中画出两个指向黄色盒子的箭头。我尝试使用 create_line 绘制对角线,但没有奏效。谁能建议我画这些箭头的任何方法。
使用 create_line 时的错误消息是:AttributeError: '_tkinter.tkapp' object has no attribute 'create_line'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from tkinter import *
import tkinter as tk


window = Tk()
window.geometry("900x500")
window.configure(background='red')
window.title("Theoretical")

label1 = Label(window, text="Hess' cycle of combustion", fg="black", bg="red", font=("Comic Sans MS", 20))
label1.pack()    

text1 = Text(window, width=20, height = 1, bg="blue")
text1.place(x=200, y=100)

window.create_line(0, 0, 200, 100)
window.create_line(0, 100, 200, 0, fill="white")

text2 = Text(window, width=20, height = 1, bg="green")
text2.place(x=520, y=100)

text3 = Text(window, width=20, height = 1, bg="yellow")
text3.place(x=370, y=250)

##    arrow = Label(window, width=13,height = 1, text ="-------------->", bg="lawn green", font=("Helvetica", 20))
##    arrow.place(x= 330, y=90)

global textbox
textbox = Text(window, width=400, height=10)
textbox.place(x=0, y= 365)


tkinter 行有一个 arrow 选项;但是,正如评论中指出的那样,create_line 是一种 Canvas 方法:因此您必须使用 tk.Canvas 对象来绘制线条:

这个最小的例子告诉你如何:

1
2
3
4
5
6
7
8
9
10
import tkinter as tk

window = tk.Tk()

canvas = tk.Canvas(window)
canvas.pack()

canvas.create_line(0, 0, 200, 100, arrow=tk.LAST)

window.mainloop()

enter

请注意,为避免"难以修复"的问题和意外行为,通常建议不要在主命名空间中导入模块(即不要 from tkinter import *),并且不要混合几何管理器((i /e 不要在同一个应用程序中使用 .place.pack)

编辑:

为了在画布上放置小部件,您必须使用 Canvas.create_window() 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import tkinter as tk

window = tk.Tk()
window.geometry("600x600")

canvas = tk.Canvas(window, width=600, height=600)

label_1 = tk.Label(window, text ="from here", anchor = tk.W)
label_1.configure(width = 10, activebackground ="#33B5E5", relief = tk.FLAT)
label_1_window = canvas.create_window(280, 0, anchor=tk.NW, window=label_1)

label_2 = tk.Label(window, text ="to there", anchor = tk.W)
label_2.configure(width = 10, activebackground ="#33B5E5", relief = tk.FLAT)
label_2_window = canvas.create_window(280, 310, anchor=tk.NW, window=label_2)

canvas.pack()
canvas.create_line(300, 40, 300, 300, arrow=tk.LAST)


window.mainloop()

enter