The following programme is supposed to draw a line, but I don’t see it.
from tkinter import *
root = Tk()
canvas = Canvas()
root.geometry("500x500")
root.mainloop()
tkinter.colorchooser
canvas.create_line(15, 25, 200, 25, width=15)
canvas.pack()
What am I missing?
MRAB
(Matthew Barnett)
May 11, 2023, 6:39pm
2
You’re not drawing the line and adding the canvas until after mainloop
returns, which happens when you close the window.
tjreedy
(Terry Jan Reedy)
May 11, 2023, 7:29pm
3
Minimal example:
from tkinter import *
root = Tk()
root.geometry("500x500")
canvas = Canvas(root)
canvas.pack()
canvas.create_line(15, 25, 200, 25, width=15)
#root.mainloop()
When run from an IDLE editor, omit the mainloop call and one can interactively enter more tkinter statements in Shell at the >>> prompt. A great way to explore the effect of different functions.
That’s what the problem was. the root.mainloop() was in the wrong place.
Thank you for the help.
Now, how do I draw functions like: y = sin(x)?
tjreedy
(Terry Jan Reedy)
May 11, 2023, 10:47pm
5
Plot points with create_text or multipoint line with create_line(x1, y1, …, xn, yn).