The example GUI in the imported TkTable module works fine. But when I try to use TkTable in my own work, every cell in the table is empty. My test code is below
from Tkinter import *
import TkTable
class App:
def __init__(self, master):
"""master is the top level window"""
master.geometry(("%dx%d")%(250,60))
master.title("Test")
frame = Frame(master)
frame.pack()
var = TkTable.ArrayVar(frame)
for y in range(6):
for x in range(6):
index = "%i,%i" % (y, x)
var[index] = index
label = Label(frame, text="test2")
label.pack(side = 'top', fill = 'x')
quit = Button(frame, text="QUIT", command=root.destroy)
quit.pack(side = 'bottom', fill = 'x')
test = TkTable.Table(frame,
rows=6,
cols=6,
state='disabled',
width=6,
height=6,
titlerows=1,
titlecols=0,
roworigin=0,
colorigin=0,
selectmode='browse',
selecttype='row',
rowstretch='unset',
colstretch='last',
flashmode='on',
variable=var,
usecommand=0)
test.pack(expand=1, fill='both')
test.tag_configure('sel', background = 'yellow')
test.tag_configure('active', background = 'blue')
test.tag_configure('title', anchor='w', bg='red', relief='sunken')
root = Tk()
app = App(root)
root.mainloop()
The window displayed has a table with all empty cells.
I’m using Python 2.6, Mac OS 10.6, and Eclipse PyDev though I don’t think any of that matters. What I think does matter, but I can’t pinpoint why, is that the example code ( http://tkinter.unpy.net/wiki/TkTableWrapper ) provided in the TkTable module I downloaded from SourceForge runs the table in a slightly different way. It has:
if __name__ == '__main__':
sample_test()
where sample_test() contains
root = Tk()
and
root.mainloop()
Why does that example method work but mine does not? Thank you in advance for any help you can offer.
You may want to read up on local variables versus global variables versus instance variables!
Replace all occurrences of
varwithself.var.