Why doesn't a line appear on my Tkinter canvas?

Viewed 39

I am trying to draw a simple line in Tkinter using a Canvas. I have this code so far:

from tkinter import *
from tkinter.ttk import *
rt = Tk()
rt.geometry('500x500')
head = Canvas(rt, width=500, height=100)
head.create_line(1, 99, 499, 99)
rt.mainloop()

When I run the program I only get a plain white screen. Why doesn't the line appear?

1 Answers

As @jasonharper mentioned, you aren't actually adding the canvas widget to the UI with a geometry manager like pack(), place() or grid(). Try...

import tkinter as tk  # avoid star imports if you can!
from tkinter import ttk
rt = tk.Tk()
rt.geometry('500x500')

head = tk.Canvas(rt, width=500, height=100)
head.pack(expand=True, fill=tk.BOTH)  # <- add this
head.create_line(1, 99, 499, 99)

rt.mainloop()
Related