Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73200 > unrolled thread

Lines on a tkinter.Canvas

Started byPedro Izecksohn <izecksohn@yahoo.com>
First post2014-06-11 18:57 -0700
Last post2014-06-13 12:31 +1200
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Lines on a tkinter.Canvas Pedro Izecksohn <izecksohn@yahoo.com> - 2014-06-11 18:57 -0700
    Re: Lines on a tkinter.Canvas Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-06-12 23:38 +1200
      Re: Lines on a tkinter.Canvas Terry Reedy <tjreedy@udel.edu> - 2014-06-12 13:44 -0400
      Re: Lines on a tkinter.Canvas Pedro Izecksohn <izecksohn@yahoo.com> - 2014-06-12 13:33 -0700
      Re: Lines on a tkinter.Canvas Pedro Izecksohn <izecksohn@yahoo.com> - 2014-06-12 16:11 -0700
        Re: Lines on a tkinter.Canvas Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-06-13 12:31 +1200

#73200 — Lines on a tkinter.Canvas

FromPedro Izecksohn <izecksohn@yahoo.com>
Date2014-06-11 18:57 -0700
SubjectLines on a tkinter.Canvas
Message-ID<mailman.11027.1402548495.18130.python-list@python.org>
  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. How should I do this? Canvas has no method create_pixel or create_point.

[toc] | [next] | [standalone]


#73216

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-06-12 23:38 +1200
Message-ID<bvthovFg8ldU1@mid.individual.net>
In reply to#73200
Pedro Izecksohn wrote:
> The Canvas' method create_line turns on at least 2 pixels. But I want to turn
> on many single pixels on a Canvas.

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.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#73231

FromTerry Reedy <tjreedy@udel.edu>
Date2014-06-12 13:44 -0400
Message-ID<mailman.11038.1402595064.18130.python-list@python.org>
In reply to#73216
On 6/12/2014 7:38 AM, Gregory Ewing wrote:
> Pedro Izecksohn wrote:
>> The Canvas' method create_line turns on at least 2 pixels. But I want
>> to turn
>> on many single pixels on a Canvas.
>
> 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.

Pedro, the tkinter canvas is a vector graphics canvas, not a bitmap 
image canvas as in paint programs.

> In that case, you might want to use a different approach,
> such as creating an image and telling the canvas to display
> the image.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#73240

FromPedro Izecksohn <izecksohn@yahoo.com>
Date2014-06-12 13:33 -0700
Message-ID<mailman.11044.1402612443.18130.python-list@python.org>
In reply to#73216
----- Original Message -----

> From: Gregory Ewing
> To: python-list@python.org
> Sent: Thursday, June 12, 2014 8:38 AM
> Subject: Re: Lines on a tkinter.Canvas
> 
> Pedro Izecksohn wrote:
>>  The Canvas' method create_line turns on at least 2 pixels. But I want 
> to turn
>>  on many single pixels on a Canvas.
> 
> You could try using a 1x1 rectangle instead.

pedro@microboard:~/programming/python/tk/canvas$ diff old/testing.001.py testing.py
19c19
<     self.canvas.create_line (p.x, p.y, p.x, p.y, fill = color)
---
>     self.canvas.create_rectangle (p.x, p.y, p.x, p.y, fill = color)

  The result that I got is: The line drawn by it is not shown.

> 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.

  I did not try this yet.

[toc] | [prev] | [next] | [standalone]


#73241

FromPedro Izecksohn <izecksohn@yahoo.com>
Date2014-06-12 16:11 -0700
Message-ID<mailman.11045.1402614834.18130.python-list@python.org>
In reply to#73216
----- Original Message -----

> From: Gregory Ewing
> To: python-list@python.org
> Cc: 
> Sent: Thursday, June 12, 2014 8:38 AM
> Subject: Re: Lines on a tkinter.Canvas
> 
> Pedro Izecksohn wrote:
>>  The Canvas' method create_line turns on at least 2 pixels. But I want 
> to turn
>>  on many single pixels on a Canvas.
> 
> 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.

  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()

[toc] | [prev] | [next] | [standalone]


#73244

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-06-13 12:31 +1200
Message-ID<bvuv40FpihtU1@mid.individual.net>
In reply to#73241
Pedro Izecksohn wrote:
>   Thank you Greg. Your second approach works and the script became:

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.

-- 
Greg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web