<div> </div>
<div> This sounds like a great use for **the canvas widget.**</div>
<div> 1. Draw layout with canvas lines and rectangles.<br> 2. Draw text representing sensor readings onto canvas.<br> 3. Refresh display by reconfiguring text, once a second.</div>
<div><br> The great thing about canvas is that -- once you've got<br> text placed on it, you can just alter the text via the<br> canvas.itemconfigure method.</div>
<div><br> Here's a rendering of your program:</div>
<div>------------------------------------------------------------------------</div>
<div>from tkinter import *</div>
<div><br>class BasicCanvasFrame1(Canvas):</div>
<div> """Basic canvas frame.</div>
<div> (Strictly, NOT a Frame -- but we're using it sort of like one.)</div>
<div> For convenience, there is a size parameter offering typical sizes:<br> 320x200 "CGA"<br> 640x480 "VGA"<br> 1024x768 "XGA"<br> 1280x1024 "SVGA"<br>
"FULL" -- full screen mode</div>
<div> For organizing's sake, the BasicCanvasFrame suggests a function,<br> "draw," for drawing onto it with.</div>
<div> Also, there are some common event registrations, so that you do not<br> need to register them yourself:<br> * mouse clicks,<br> * mouse motion,<br> * key presses<br> """</div>
<div> def __init__(self, title="Untitled Frame",<br> size="VGA", width=None, height=None,<br> enable_per_second=False,<br> master=None,<br> external_delegate=None):<br>
self.external_delegate = external_delegate<br> # 1. Initialize Frame<br> size = size.upper()<br> if size != "FULL":<br> if width is None:<br> width = {"CGA": 320, "VGA": 640,<br>
"XGA": 1024, "SVGA": 1280}[size]<br> if height is None:<br> height = {"CGA": 200, "VGA": 480,<br> "XGA": 768, "SVGA": 1024}[size]<br>
Canvas.__init__(self, master, width=width, height=height)<br> self.master.title(title)<br> else:<br> Canvas.__init__(self, master)<br> (width, height) = tk_fullscreen(self)<br>
self.configure(width=width, height=height)<br> self.pack(anchor=N+W, fill='both', expand='yes')</div>
<div> # Fix origin displayer, per:<br> # <a href="http://tkinter.unpythonic.net/wiki/Widgets/Canvas?highlight=(canvas">http://tkinter.unpythonic.net/wiki/Widgets/Canvas?highlight=(canvas</a>)<br> self.xview_moveto(0)<br>
self.yview_moveto(0)</div>
<div> # 2. Bind<br> self.bind("<Button-1>", self.left_click)<br> self.bind("<Button-3>", self.right_click)<br> #self.bind("<Any-KeyPress>", self.keypress)<br>
self.bind_all('<Key>', self.keypress)<br> self.bind_all('<Key-Alt_L>', self.keypress)<br> self.bind_all('<Key-Alt_R>', self.keypress)<br># self.bind_all('<Key->', self.keypress)<br>
self.bind("<Motion>", self.motion)</div>
<div> # 3. Draw<br> self.wait_visibility()<br> self.draw()<br> if external_delegate is not None:<br> external_delegate.draw(self)</div>
<div> # 4. Turn on per-second events, if requested<br> if enable_per_second:<br> self.after(1000, self._per_second)</div>
<div> def _per_second(self):<br> self.per_second()<br> self.after(1000, self._per_second)</div>
<div> def draw(self):<br> pass</div>
<div> def left_click(self, evt):<br> pass</div>
<div> def right_click(self, evt):<br> pass</div>
<div> def keypress(self, evt):<br> """Key pressed.</div>
<div> evt.keysym -- super-friendly key symbol<br> evt.char -- For textual characters, this is the char.<br> evt.keycode -- For recognizing things like...<br> """<br> pass</div>
<div> def motion(self, evt):<br> pass</div>
<div> def per_second(self):<br> pass</div>
<div><br>class MickSulleysTemperatureSensor(BasicCanvasFrame1):<br> <br> def __init__(self, **args):<br> BasicCanvasFrame1.__init__(<br> self,<br> title="Mike Sulley's Temperature Sensor",<br>
width=130*3,<br> height=75*3,<br> enable_per_second=True)<br> <br> def draw(self):<br> self.create_rectangle(45*3,35*3,85*3,55*3, outline="blue")<br> for (x1,y1,x2,y2) in [(15*3,15*3, 55*3,15*3),<br>
(55*3,15*3, 55*3,35*3),<br> (15*3,45*3, 45*3,45*3),<br> (85*3,45*3,105*3,45*3)]:<br> self.create_line(x1,y1,x2,y2)<br> self.temperature_item = {}<br>
for (x,y,name) in [( 15*3,15*3, "A"),<br> ( 55*3,35*3, "B"),<br> ( 15*3,45*3, "C"),<br> ( 45*3,45*3, "D"),<br>
( 85*3,45*3, "E"),<br> (105*3,45*3, "F")]:<br> text_item = self.create_text(x+5,y+2, text="<"+name+">",<br> anchor=NW)<br>
self.temperature_item[name] = text_item<br> <br> def get_temperature(self, sensor_name):<br> """Put your code in here."""<br> import random<br> return random.randint(72,95)<br>
<br> def per_second(self):<br> for (sensor_name, text_item) in self.temperature_item.items():<br> cur_temperature = self.get_temperature(sensor_name)<br> self.itemconfigure(text_item,<br>
text="%s: %d" % (sensor_name,<br> cur_temperature))</div>
<div>MickSulleysTemperatureSensor().mainloop()</div>
<div>------------------------------------------------------------------------</div>
<div><br> (This is longer than is strictly necessary; I'm reusing<br> BasicCanvasFrame1 from my notes to make this quickly.)</div>
<div> The main points of interest are:</div>
<div> * MickSulleysTemperatureSensor.draw:<br> (the drawing code)</div>
<div> * MickSulleysTemperatureSensor.per_second:<br> (the itemconfigure line)</div>
<div> * BasicCanvasFrame1.__init__:<br> (Canvas initialization, packing code, Canvas.after<br> initialization)</div>
<div> * BasicCanvasFrame1._per_second:<br> (Canvas.after continuity)</div>
<div><br> Feel free to adapt and reuse the code here.</div>
<div> (If you experiment with Canvas, I might suggest using<br> BasicCanvasFrame1 yourself -- it has made my own<br> experimentation a lot easier.)</div>
<div> </div>
<div> Sincerely,</div>
<div> Lion Kimbro</div>
<div> </div>
<div> </div>
<div class="gmail_quote">On Mon, Oct 19, 2009 at 8:29 AM, MickSulley <span dir="ltr"><<a href="mailto:mick@sulley.info">mick@sulley.info</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote"><br>Hi,<br><br>I am new to TKinter and struggling somewhat.<br><br>I have a Python program that reads a number of temperatures from sensors in<br>
tanks and pipework, that is working fine. I now want to display those<br>temperatures, ideally a simple diagram of the pipework with boxes displaying<br>the values. I don't need any user input, it just runs forever once started<br>
<br>My initial thought was to use a text widget for the whole page and update<br>the areas that show the values.<br><br>Is that the best way to do it? Can anyone give me some pointers on<br>constructing the program?<br><br>
Thanks<br><br>Mick<br><font color="#888888">--<br>View this message in context: <a href="http://www.nabble.com/Newbie-question---displaying-data-tp25960335p25960335.html" target="_blank">http://www.nabble.com/Newbie-question---displaying-data-tp25960335p25960335.html</a><br>
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.<br><br>_______________________________________________<br>Tkinter-discuss mailing list<br><a href="mailto:Tkinter-discuss@python.org">Tkinter-discuss@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tkinter-discuss" target="_blank">http://mail.python.org/mailman/listinfo/tkinter-discuss</a><br></font></blockquote></div><br>