即使使用IntVar(),我也无法更新标签内的变量。我可能正在做一些长期错误的事情。试图做一个Nim游戏,通过改变变量来更新标签。
# ministry of trade
from tkinter import *
import random
# create root window
root = Tk()
# ministry of variables
lst_choice = [1,2]
lst_player = [1,2]
player=random.choice(lst_player) #set random player
playervar = StringVar()
playervar.set(player)
# Set the initial state
state=7 # number of objects
statevar = IntVar()
statevar.set(state)
winner=0
winnervar = IntVar()
winnervar.set(winner)
move=0
movevar = IntVar()
movevar.set(move)
def move1():
# move is assigned
global statevar
global playervar
global winnervar
if statevar.get()>0:
statevar.set(statevar.get() - 1)
print(statevar.get()) #for testing only
return statevar
if statevar.get()==0: # check win status - win, lose, stalemate
winnervar.set(playervar.get())
print(winnervar.get()) #for testing only
return winnervar
if playervar.get()==1: # switch players 2->1, 1->2 go back to the valid move line
playervar.set(2)
print(playervar.get()) #for testing only
return playervar
else:
playervar.set(1)
print(playervar.get()) #for testing only
return playervar
root.update_idletasks()
def move2():
return
# Holy Roman Empire of the Widget Nation
# electorate of Label
labelAantalMunten = Label(root, text=("Aantal munten op stapel ",statevar.get())) #state
labelAantalMunten.pack() # orders to add the widget label to the root window and display it
labelWelkSpeler = Label(root, text=("Nu is speler ",playervar.get()," aan de beurt")) #player
labelWelkSpeler.pack()
labelWelkWinaar = Label(root, text=("Speler ",winnervar.get()," heeft gewonnen!")) #winner
labelWelkWinaar.pack()
# electorate of Button
button1 = Button(root, text="neem 1 steen", command=move1)
button1.pack()
button2 = Button(root, text="neem 2 stenen", command=move2)
button2.pack()
# start the main events loop
root.mainloop()
答案 0 :(得分:1)
以下是每次按下按钮后应更新标签的示例。
textvariable
代替text
。 StringVar
变量,并在函数mov1中更新它。玩得开心!
示例代码
# ministry of trade
from tkinter import *
import random
# create root window
root = Tk()
# Set the initial state
state=7 # number of objects
statevar = IntVar()
statevar.set(state)
def move1():
# move is assigned
global statevar
if statevar.get()>0:
print('statevar.get()>0')
print(statevar.get()) #for testing only
statevar.set(statevar.get() - 1)
print(statevar.get()) #for testing only
a = "Aantal munten op stapel " + str(statevar.get())
print('a = {}'.format(a))
text1.set(a)
return statevar
a = "Aantal munten op stapel " + str(statevar.get())
text1=StringVar()
text1.set(a)
labelAantalMunten = Label(root, textvariable=text1) #state
labelAantalMunten.pack() # orders to add the widget label to the root window and display it
# electorate of Button
button1 = Button(root, text="neem 1 steen", command=move1)
button1.pack()
# start the main events loop
root.mainloop()
答案 1 :(得分:0)
labelWelkSpeler = Label(root, text=("Nu is speler ",playervar.get()," aan de beurt")) #player
这将获取playervar ONCE的值(在您创建标签时),并在此时构建标签的文本。 (并且可能没有构建你真正想要的文本,因为你写的是一个元组结构,而不是字符串连接。)每次playervar改变时,它都不会神奇地导致重新运行这个代码!
从变量中自动更新Label的唯一方法是将其指定为textvariable=
选项,而不是text=
。变量必须保持要显示的整个文本,因此它几乎肯定是StringVar,而不是IntVar。你需要将玩家得分保持在一个单独的变量(一个普通的Python变量,最有可能),然后.set()StringVar自己随时改变。