tam*_*ini -2 python tkinter tkinter-canvas
我需要从指向黄色框的蓝色和绿色框绘制两个箭头。我尝试使用 create_line 绘制对角线,但没有用。任何人都可以建议我可以绘制这些箭头的任何方法。使用 create_line 时的错误消息是:AttributeError: '_tkinter.tkapp' object has no attribute 'create_line'
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)
Run Code Online (Sandbox Code Playgroud)
tkinter 线有一个arrow选项;然而,正如 te 评论中指出的那样,create_line是一种Canvas方法:因此,您必须使用tk.Canvas对象来绘制线条:
这个最小示例向您展示了如何:
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()
Run Code Online (Sandbox Code Playgroud)
请注意,为了避免“难以修复”的问题和意外行为,通常建议不要在主命名空间中导入模块(i/e do not from tkinter import *),并且不要混合几何管理器((i/e do not use .placeand.pack在同一个应用程序中)
为了在画布上放置小部件,您必须使用以下Canvas.create_window()方法:
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7094 次 |
| 最近记录: |