tkinter changeable value on canvas

后端 未结 2 854
轮回少年
轮回少年 2021-01-27 09:16

I want to change the text label on a canvas when buttons are clicked. I want to increase the label by 10 if the button \"up\" is clicked and decrease by 10 if the button \"down\

2条回答
  •  面向向阳花
    2021-01-27 09:26

    You can use the documented itemconfigure method of the canvas to change any configuration option of any object on the canvas.

    For example, you could write a method named change_consumption that takes a parameter for how much to change the value by, and it can use itemconfigure to change what appears on the canvas:

    def change_consumption(self, amount):
        self.consumption += amount
        self.canvas.itemconfigure(self.cons, text=self.consumption)
    

    You would bind to this function like this for the "up" button; for "down" you would pass -10:

    self.but.bind("", lambda event: self.change_consumption(10))
    

提交回复
热议问题