使用此代码,框显示全部0。我不确定为什么它没有在列表中显示序列。它应该显示" 1"在[0,0]," 3"在[0,1]," 2"在[0,2] ......等等,直到填满9个方框中的所有9个数字。
import tkinter as tk
the_list = (1, 3, 2, 4, 12, 56, 7, 10, 19)
var_categories = {}
def main():
root = tk.Tk()
root.title("class basic window")
root.geometry("500x300")
root.config(background="LightBlue4")
app = Application(root)
root.mainloop()
class Application(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg="ivory2", bd=2, relief=tk.RAISED)
self.parent = parent
self.pack(fill=tk.BOTH, expand=1)
self.initUI()
def initUI(self):
iterator = iter(the_list)
for r in range(3):
for c in range(3):
item = next(iterator)
self.labelVariable = tk.IntVar()
self.label = tk.Label(self, textvariable=self.labelVariable, relief="ridge",width=8, height=3 )
self.label.grid(row=r, column=c, sticky='news')
var_categories[item] = self.labelVariable
if __name__ == '__main__':
main()
答案 0 :(得分:0)
主要问题在于函数NamedImage
:当您创建initUI
时,您必须使用其IntVar
方法设置其值。另外,我猜你的set
词典应该用它在矩阵上的位置来索引,而不是矩阵单元格的内容,我是对的吗?以下是修改后的函数,它提供了您想要的输出:
var_categories