0

I need to draw two arrows from the blue and green box pointing to the yellow box. I tried drawing diagonal lines using create_line but it didn't work. can anyone please suggest any ways I could draw these arrows. the error message when using create_line is: 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)
tamilini
  • 53
  • 1
  • 2
  • 10
  • Please explain _and_ provide how exactly the Canvas method has failed. – Nae Feb 19 '18 at 21:07
  • 1
    `create_line` is a method on a canvas. I don't see a canvas in your code. – Bryan Oakley Feb 19 '18 at 22:07
  • 1
    In `tkinter`, `Canvas` line objects have an `arrowhead` option that you might be able to use. Here's some [documentation](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_line.html). – martineau Feb 19 '18 at 22:07
  • i added the error message when i added create_line which was AttributeError: '_tkinter.tkapp' object has no attribute 'create_line' – tamilini Feb 20 '18 at 11:05

1 Answers1

12

tkinter lines have an arrow option; however, as pointed out in te comments,create_line is a Canvas method: you must therefore use a tk.Canvas object to draw lines:

This minimum example shows you how:

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 image description here

Please note that to avoid "hard to fix" problems and unexpected behavior, it is usually recommended not to import modules in the main namespace (i/e do not from tkinter import *), and not to mix geometry managers ((i/e do not use .place and .pack in the same app)

Edit:

In order to place widgets on a canvas, you must use the Canvas.create_window() method:

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 image description here

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • thank you... but i am still not sure how to include it in my program. do i create this window within my window? – tamilini Feb 20 '18 at 14:42
  • @tamilini Replace your `....create_line(...)` lines with the lines above that starts with `canvas...`. – Nae Feb 21 '18 at 19:02