导入的 TkTable 模块中的示例 GUI 工作正常。但是当我尝试在自己的工作中使用 TkTable 时,表中的每个单元格都是空的。我的测试代码如下
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()
显示的窗口有一个包含所有空单元格的表格。
我正在使用 Python 2.6、Mac OS 10.6 和 Eclipse PyDev,尽管我认为这些都不重要。我认为重要的是,但我无法查明原因,我从 SourceForge 下载的 TkTable 模块中提供的示例代码 ( http://tkinter.unpy.net/wiki/TkTableWrapper ) 以稍微不同的方式运行该表。它有:
if __name__ == '__main__':
sample_test()
其中sample_test()包含
root = Tk()
和
root.mainloop()
为什么这个示例方法可以工作,但我的却不行?预先感谢您提供的任何帮助。
最佳答案
您可能需要阅读局部变量与全局变量与实例变量!
将所有出现的 var
替换为 self.var
。
关于python - tktable 中的单元格为空白而不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10827758/