使用TkInter更改标签文本

时间:2018-03-18 22:57:29

标签: python python-3.x tkinter

我正在为8-Puzzle问题做一个简单的算法,但我想在GUI中显示它。

这是我解决问题的循环:

while puzzle != puzzleGoals:
cont += 1
nRow = row(puzzle.index(0))
resolve(nRow)

解决是完成所有工作的功能。它工作正常并给出了正确的结果。

我可以按照我想要的顺序创建一个屏幕

a1 = Label(root, text = puzzle[0]).grid(row = 0, column = 0, sticky= W)
a2 = Label(root, text = puzzle[1]).grid(row = 0, column = 1, sticky= W)
a3 = Label(root, text = puzzle[2]).grid(row = 0, column = 2, sticky= W)

b1 = Label(root, text = puzzle[3]).grid(row = 1, column = 0, sticky= W)
b2 = Label(root, text = puzzle[4]).grid(row = 1, column = 1, sticky= W)
b3 = Label(root, text = puzzle[5]).grid(row = 1, column = 2, sticky= W)

c1 = Label(root, text = puzzle[6]).grid(row = 2, column = 0, sticky= W)
c2 = Label(root, text = puzzle[7]).grid(row = 2, column = 1, sticky= W)
c3 = Label(root, text = puzzle[8]).grid(row = 2, column = 2, sticky= W)

looks like this

那么,我怎样才能在每次拼图的新组合时更改标签文本? 有一种" label.setText(" abc")的可能性? 以及如何在while循环中输入root.mainloop()?还是不必要的?

非常感谢

1 个答案:

答案 0 :(得分:0)

使用StringVar并将其设置为标签textvariable关键字,然后使用StringVar的.set方法更改文本

相关问题