Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the Tkinter canvas lines jagged?

The lines drawn on a Tkinter.Canvas are not smooth. How can they be made smooth?

Here's what I tried:

 from Tkinter import *
 root = Tk()
 cv = Canvas(root,bg = 'white')
 rt1 = cv.create_rectangle(10,10,110,110,width = 8,tags = ('r1','r2','r3'))

 def printRect(event):
     print 'rectangle'
 def printLine(event):
     print 'line'

 cv.tag_bind('r1','<Button-1>',printRect)
 cv.tag_bind('r1','<Button-3>',printLine)
 cv.create_line(10,20,200,200,width = 5,tags = 'r1')
 cv.pack()
 root.mainloop()

Here's what it looks like:

tkinter output from sample code

like image 916
user1653363 Avatar asked Sep 15 '12 09:09

user1653363


People also ask

Which is the correct way to draw a line in Canvas tkinter?

To draw a line on a Canvas, we can use create_line(x,y,x1,y1, **options) method. In Tkinter, we can draw two types of lines: simple and dashed.

What is the difference between Canvas and frame in tkinter?

A Frame is designed to be a parent container of other widgets. A Canvas is like a canvas that you can draw somethings on it like lines, circles, text, etc. A Canvas can be used as a parent container as well but it is not designed for that at the first place.

How do I reset my python Canvas?

Build A Paint Program With TKinter and Python While creating a canvas in tkinter, it will effectively eat some memory which needs to be cleared or deleted. In order to clear a canvas, we can use the delete() method. By specifying “all”, we can delete and clear all the canvas that are present in a tkinter frame.

How does tkinter Canvas work?

A tkinter canvas can be used to draw in a window. Use this widget to draw graphs or plots. You can even use it to create graphical editors. You can draw several widgets in the canvas: arc bitmap, images, lines, rectangles, text, pieslices, ovals, polygons, ovals, polygons, and rectangles.


2 Answers

Tkinter graphics are not anti-aliased which is why the diagonal line appears jagged. There may be a platform specific work-around like this one I found titled Drawing Anti-Aliased Graphics Under Tkinter/Windows to provide the functionality you desire.

like image 77
martineau Avatar answered Sep 17 '22 08:09

martineau


You might try to do some antialiasing of the poor, by drawing a clearer colored one pixel larger second line before (under) the first one.

like image 21
Camion Avatar answered Sep 19 '22 08:09

Camion