The Tkinter 3000 SimplePlot Widget
July 16, 2002 | Fredrik Lundh
This note describes how to create simple “plot” widgets with Tkinter 3000. In all examples, there’s an external source that feeds coordinate pairs to the widget, and the widget draws a “dot” on the screen for each coordinate.
As a reference, we’ll use a Canvas-based implementation and a simple program using 5000 random coordinates:
import Tkinter class SimplePlot(Tkinter.Canvas): def plot(self, x, y): self.create_line((x, y, x+1, y), fill="black") # # test program import random, time root = Tkinter.Tk() root.title("demoSimplePlot") widget = SimplePlot(root) widget.pack(fill="both", expand=1) widget.update() # display the widget data =  for i in range(5000): data.append((random.randint(0, 200), random.randint(0, 200))) t0 = time.time() for x, y in data: widget.plot(x, y) widget.update() # make sure everything is drawn print time.time() - t0, "seconds" root.mainloop()
On my slowest reference machine, it takes about 3.5 seconds to create all line fragments and display the data.
Just the dots…
The first Tkinter 3000 example simply draws dots in the foreground color, as they arrive. To draw dots, we can either use a pen and draw short lines, or use a brush and draw a small rectangle. It turns out that the latter is a bit faster; all in all, the following test program needs about 0.65 seconds to plot the same data.
class SimplePlot(Widget): ui_option_foreground = "black" ui_option_width = 200 ui_option_height = 200 def ui_handle_config(self): self.brush = self.ui_brush(self.ui_option_foreground) return int(self.ui_option_width), int(self.ui_option_height) def plot(self, x, y): self.ui_draw.rectangle((x, y, x+1, y+1), self.brush)
However, unlike the Canvas, this version does not remember its contents. If you iconify the window, or cover it with another window, the dots will have disappeared when the window is display again.To be continued…