[英]Change the label text using TkInter
I'm doing a simple algorithm for the 8-Puzzle problem, but I want to show it in a GUI. 我正在为8-Puzzle问题做一个简单的算法,但是我想在GUI中显示它。
This, is my loop to solve the problem: 这是我解决问题的循环:
while puzzle != puzzleGoals:
cont += 1
nRow = row(puzzle.index(0))
resolve(nRow)
Resolve is the function who do all the work. 解决是负责所有工作的职能。 It's working fine and gives the right result. 它工作正常,并给出正确的结果。
I can create a screen with the order that I want 我可以根据需要创建一个屏幕
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)
So, how can I change the labels texts every time that's a new combination of the puzzle? 那么,每当这是拼图的新组合时,如何更改标签文本? There's a possibility of a kind of "label.setText("abc")? And how to input the root.mainloop() inside the while loop? Or its unnecessary? 有没有一种“ label.setText(” abc“)?以及如何在while循环中输入root.mainloop()还是不必要的?
Many thanks 非常感谢
使用StringVar并将其设置为标签textvariable关键字,然后使用StringVar的.set方法更改文本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.