[Tutor] Tkinter, widgets not displaying...

Hugo González Monteverde hugonz-lists at h-lab.net
Fri Feb 10 06:24:07 CET 2006


Hi All,

I wrote a small turn delivering graphical app that is supposed to 
display turns in a queue.

If I instantiate the class and call its methods, thus displaying strings 
in several canvases, from the interactive prompt, everything works fine. 
  If I do it when running the script as a programs, nothing gets displayed.

If I call mainloop() at the end of my program, everything gets 
displayed, but then I cannot use my own function classes. This program 
will never respond to events, but just get called from a "queue manager" 
script for display...

Here's the code:

#!/usr/bin/env python
from __future__ import division
import Tkinter
import tkFont
from Tkconstants import *


from time import sleep

class TurnQueue(Tkinter.Frame):
     def __init__(self, master=None, pw=60, ph=60, panels=4):
         self.ph = ph
         self.pw = pw
         self.panels = panels

         Tkinter.Frame.__init__(self, master)
         self.createWidgets()
         self.pack()

     def createWidgets(self):
         """Create four canvases and a warning window."""
         self.canvases =  [Tkinter.Canvas(height=self.ph, width=self.pw, 
borderwidth=1, background = '#FFFFFF') for i in range(self.panels)]
         self.textbox_ids = []

         # display them all, initialized
         for i in self.canvases:
             id = i.create_text(self.pw//2, self.ph//2, text='', 
font=('bitstream charter', self.ph, 'bold'), fill='red')
             self.textbox_ids.append(id)
             i.pack()

     def write_panel(self, panel_no, newtext):
         """writepanel(self, panel, newtext)
         Set panel <panel> to text <newtext>"""
 
self.canvases[panel_no].itemconfigure(self.textbox_ids[panel_no], 
text=newtext)

     def read_panel(self, panel_no):
         """textcontents <- read_panel(self, panel_no)
         Read panel text contents"""
 
return(self.canvases[panel_no].itemcget(self.textbox_ids[panel_no], 'text'))

     def insert(self, turn_string):
         """Insert a new turn into the queue, move the rest upwards, 
delete oldest."""
         current_values = [self.read_panel(i) for i in 
range(len(self.canvases))]
         next_values = current_values[:]
         next_values[:-1] = current_values[1:]
         next_values[-1] = turn_string

         # update all panel values
         for i in range(len(self.canvases)):
             self.write_panel(i, next_values[i])

if __name__ == "__main__":
     myturns = TurnQueue(pw=200, ph=100, panels=4)

     for i in range(100):
         myturns.insert(str(i))
         sleep(0.2)


More information about the Tutor mailing list