Lines on a tkinter.Canvas

Discussion in 'Python' started by Pedro Izecksohn, Jun 12, 2014.

  1.   The code available from:
    http://izecksohn.com/pedro/python/canvas/testing.py
      draws 2 horizontal lines on a Canvas. Why the 2 lines differ on thickness and length?

      The Canvas' method create_line turns on at least 2 pixels. But I want to turn on many single pixels on a Canvas. Howshould I do this? Canvas has no method create_pixel or create_point.
     
    Pedro Izecksohn, Jun 12, 2014
    #1
    1. Advertisements

  2. You could try using a 1x1 rectangle instead.

    However, be aware that either of these will use quite a
    lot of memory per pixel. If you are drawing a very large
    number of pixels, this could cause performance problems.
    In that case, you might want to use a different approach,
    such as creating an image and telling the canvas to display
    the image.
     
    Gregory Ewing, Jun 12, 2014
    #2
    1. Advertisements

  3. Pedro Izecksohn

    Terry Reedy Guest

    Pedro, the tkinter canvas is a vector graphics canvas, not a bitmap
    image canvas as in paint programs.
     
    Terry Reedy, Jun 12, 2014
    #3
  4. ----- Original Message -----
      Thank you Greg. Your second approach works and the script became:

    #!/usr/bin/python3

    import tkinter as tk

    BITMAP = '''
    #define im_width 1
    #define im_height 1
    static char im_bits[] = {
    0xff
    };
    '''

    class Point ():
      def __init__ (self, x, y):
        self.x = x
        self.y = y

    class Board (tk.Frame):
      def __init__ (self, bg,dimensions):
        tk.Frame.__init__ (self, tk.Tk())
        self.pack()
        self.canvas = tk.Canvas (self, bd = 0, bg = bg, width = dimensions.x, height = dimensions.y)
        self.canvas.pack (side = "top")
        self.objects_drawn = []
      def drawLine (self, pa, pb, color):
        self.canvas.create_line (pa.x, pa.y, pb.x, pb.y, fill = color)
      def drawPoint (self, p, color):
        bitmap = tk.BitmapImage (data=BITMAP, foreground = color)
        self.objects_drawn.append (bitmap)
        self.canvas.create_image (p.x, p.y, image = bitmap)

    dimensions = Point (500, 500)
    board = Board ('black', dimensions)
    color = 'red'
    p = Point (0, 250)
    while (p.x < dimensions.x):
      board.drawPoint (p, color)
      p.x += 1
    pa = Point (0, 350)
    pb = Point (499, 350)
    board.drawLine (pa, pb, color)
    board.mainloop()
     
    Pedro Izecksohn, Jun 13, 2014
    #4
  5. That's not really what I meant; doing it that way,
    you're still incurring the overhead of a tk canvas
    object for each point that you draw. However, if
    there are only 250 points or so, it might not matter.
     
    Gregory Ewing, Jun 13, 2014
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.