简体   繁体   中英

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.

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)

looks like this

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?

Many thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM