stringvar 如何与 textvariable 一起使用?
How does stringvar work with textvariable?
我正在编写一个国际象棋程序,但我无法输出棋子,因为我将文本从使用文本定义更改为文本变量,现在我无法使用字符串来输出棋子。
我在 for 循环中定义按钮,它在方块中不输出任何内容。密码是
def drawboard(self):
x=0
y=0
for column in range(self.n):
self.changecolours()
x=x+1
y=0
for row in range(self.n):
y=y+1
colour = self.colours[self.colourindex]
pos=(x,9-y)
buttons=(tk.Button(self.boardframe, padx=10, textvariable=lambda position=pos: self.placepieces(position), borderwidth=0, bg=colour, relief="solid", font=self.piecefont, command=lambda position=pos: self.movepiece(position) ))
buttons.grid(column=(x-1), row=(y-1), sticky="W"+"E"+"N"+"S" )
self.changecolours()
@classmethod
def placepieces(cls, position):
black=Black()
white=White()
squareposition=position
icon=tk.Stringvar("")
if squareposition in white.position.values():
for key in white.position:
value=white.position.get(key)
if value==squareposition:
if key.endswith("pawn"):
icon.set(white.pawntype[key])
elif key=="king":
icon.set(white.PIECES.get("KING"))
elif key=="queen":
icon.set(white.PIECES.get("QUEEN"))
elif key.endswith("bishop"):
icon.set(white.PIECES.get("BISHOP"))
elif key.endswith("knight"):
icon.set(white.PIECES.get("KNIGHT"))
else:
icon.set(white.PIECES.get("ROOK"))
else:
pass
elif squareposition in black.position.values():
for key in black.position:
value=black.position.get(key)
if value==squareposition:
if key.endswith("pawn"):
icon.set(black.pawntype.get(key))
elif key=="king":
icon.set(black.PIECES.get("KING"))
elif key=="queen":
icon.set(black.PIECES.get("QUEEN"))
elif key.endswith("bishop"):
icon.set(black.PIECES.get("BISHOP"))
elif key.endswith("knight"):
icon.set(black.PIECES.get("KNIGHT"))
else:
icon.set(black.PIECES.get("ROOK"))
break
else:
pass
else:
icon.set("")
return icon
How does stringvar work with textvariable?
对于 Button,您可以将文本配置为以两种方式之一显示:使用硬编码字符串(例如:text='click me'
)或使用 textvariable
属性.
textvariable
属性必须设置为特殊 tkinter 变量对象之一的实例 StringVar
、IntVar
、DoubleVar
或 BooleanVar
(例如:var=tk.StringVar(); Button(..., textvariable=var)
)。当配置了这些变量之一的实例时,按钮上的文本将显示变量的值。
在内部,当您指定 textvariable
时,tkinter 将采用该对象的字符串表示形式并在嵌入式解释器中创建一个 tcl 变量。因此,虽然它可能会接受一个标准变量、命令,甚至是一个 lambda,但它最终会创建一个具有该名称的内部 tcl 变量,并将该内部变量与底层的 tcl 小部件相关联。如果您不使用其中一个特殊变量,您将很难获取或设置该变量的值。
例如,考虑这组命令:
var = tk.StringVar(value="hello")
tk.Button(root, textvariable=var)
当该代码为 运行 时,按钮上显示的文本将为 hello
。如果您随时更改变量(例如:var.set("goodbye")
),按钮将自动更改以显示新值。
我正在编写一个国际象棋程序,但我无法输出棋子,因为我将文本从使用文本定义更改为文本变量,现在我无法使用字符串来输出棋子。 我在 for 循环中定义按钮,它在方块中不输出任何内容。密码是
def drawboard(self):
x=0
y=0
for column in range(self.n):
self.changecolours()
x=x+1
y=0
for row in range(self.n):
y=y+1
colour = self.colours[self.colourindex]
pos=(x,9-y)
buttons=(tk.Button(self.boardframe, padx=10, textvariable=lambda position=pos: self.placepieces(position), borderwidth=0, bg=colour, relief="solid", font=self.piecefont, command=lambda position=pos: self.movepiece(position) ))
buttons.grid(column=(x-1), row=(y-1), sticky="W"+"E"+"N"+"S" )
self.changecolours()
@classmethod
def placepieces(cls, position):
black=Black()
white=White()
squareposition=position
icon=tk.Stringvar("")
if squareposition in white.position.values():
for key in white.position:
value=white.position.get(key)
if value==squareposition:
if key.endswith("pawn"):
icon.set(white.pawntype[key])
elif key=="king":
icon.set(white.PIECES.get("KING"))
elif key=="queen":
icon.set(white.PIECES.get("QUEEN"))
elif key.endswith("bishop"):
icon.set(white.PIECES.get("BISHOP"))
elif key.endswith("knight"):
icon.set(white.PIECES.get("KNIGHT"))
else:
icon.set(white.PIECES.get("ROOK"))
else:
pass
elif squareposition in black.position.values():
for key in black.position:
value=black.position.get(key)
if value==squareposition:
if key.endswith("pawn"):
icon.set(black.pawntype.get(key))
elif key=="king":
icon.set(black.PIECES.get("KING"))
elif key=="queen":
icon.set(black.PIECES.get("QUEEN"))
elif key.endswith("bishop"):
icon.set(black.PIECES.get("BISHOP"))
elif key.endswith("knight"):
icon.set(black.PIECES.get("KNIGHT"))
else:
icon.set(black.PIECES.get("ROOK"))
break
else:
pass
else:
icon.set("")
return icon
How does stringvar work with textvariable?
对于 Button,您可以将文本配置为以两种方式之一显示:使用硬编码字符串(例如:text='click me'
)或使用 textvariable
属性.
textvariable
属性必须设置为特殊 tkinter 变量对象之一的实例 StringVar
、IntVar
、DoubleVar
或 BooleanVar
(例如:var=tk.StringVar(); Button(..., textvariable=var)
)。当配置了这些变量之一的实例时,按钮上的文本将显示变量的值。
在内部,当您指定 textvariable
时,tkinter 将采用该对象的字符串表示形式并在嵌入式解释器中创建一个 tcl 变量。因此,虽然它可能会接受一个标准变量、命令,甚至是一个 lambda,但它最终会创建一个具有该名称的内部 tcl 变量,并将该内部变量与底层的 tcl 小部件相关联。如果您不使用其中一个特殊变量,您将很难获取或设置该变量的值。
例如,考虑这组命令:
var = tk.StringVar(value="hello")
tk.Button(root, textvariable=var)
当该代码为 运行 时,按钮上显示的文本将为 hello
。如果您随时更改变量(例如:var.set("goodbye")
),按钮将自动更改以显示新值。