簡體   English   中英

如何在Tkinter畫布上創建文本並更改畫布中的文本?

[英]How to create text on a tkinter canvas and change the text in the canvas?

我正在使用tkinter創建游戲,並且需要更改函數中的某些文本。 如何初始化文本框並更改文本塊中的文本?

我嘗試創建文本,然后在單獨的函數中使用itemconfigure進行更新,但出現錯誤。

self.player1_troops = self.canvas.create_text(80, 140,text='')
self.player2_troops = self.canvas.create_text(1210, 140,text='')
def changeValues(self, player1, player2):
     self.canvas.itemconfigure(self.player1_troops, player1.printTroops())
     self.canvas.itemconfigure(self.player2_troops, player2.printTroops())

我希望文本塊的值發生變化,但是我收到了關於未知對象的錯誤消息。

您必須告訴itemconfigure更改哪個屬性。 您應該這樣使用它:

self.canvas.itemconfigure(self.player1_troops, text=player1.printTroops())

注意添加了text=

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM