Blake Mischley
Blake Mischley

Reputation: 55

How to make text update on the canvas after a button click in tkiner

I want to make a "shop" and once you click a certain button it will subtract however much gold it cost for that. The problem I am facing is refreshing the canvas after you click the button so it shows how much gold you have.

class Hi:
    gold=100
    def showShop(self):
        def buyArcher():
            Hi.gold = int(Hi.gold) - 10
            updateScreen(canva)

        def updateScreen(name: Canvas):
            self.name = name
            self.name.update_idletasks()

        canva = Canvas(root, width="750", height="750",bg="brown")
        button1 =Button(root,text="Hello", command=buyArcher)
        button1.configure(width=10, activebackground="#33B5E5", relief=FLAT)
        button1_window = canva.create_window(10, 10, anchor=NW, window=button1)
        label = Label(text=Hi.gold, width=10)
        label.configure(width=10, activebackground="brown", relief=FLAT)
        label_window = canva.create_window(700, 10, anchor=NW, window=label)
        canva.pack()
        root.mainloop()

I expect the gold to change from 100 to 90 on the screen after a button click

Upvotes: 1

Views: 205

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36682

Here is a little skeleton that you can maybe expand upon to build your game.

It has a class Player, a class Shop, and a class Item; it keeps a gold balance for the player, and refuses to spend when the savings are insufficient. It keeps a rudimentary item inventory for both the player and the shop; the shop cannot sell items that it does not have.

You can purchase items by clicking on the corresponding element on the canvas, and the remaining gold amount is displayed.

import tkinter as tk

class Player:
    def __init__(self, name, gold=100):
        self.name = name
        self.gold = 100
        self.items = []
    def pay_gold(self, amount):
        if self.gold >= amount:
            self.gold -= amount
            goldvar.set(self.gold)
            return True
        return False

    def __str__(self):
        return f'Player: {self.name} posessions: \n\t' + '\n\t'.join(f'{item.name}' for item in self.items) + f'\n\tgold: {self.gold}'


class Item:
    def __init__(self, name, price):
        self.name = name
        self.price = price
    def __eq__(self, other):
        return self.name == other.name


class Shop:
    def __init__(self, items):
        self.items = items[:]
    def buy_item(self, item):
        if item in self.items and player.gold >= item.price:
            if player.pay_gold(item.price):
                self.items.remove(item)
                player.items.append(item)
    def __str__(self):
        return 'shop inventory:\n\t' + '\n\t'.join(f'{item.name} at {item.price}' for item in self.items)


archer = Item('Archer', 10)
sword = Item('Sword', 15)
shop = Shop([archer, sword, archer, sword, archer, sword, archer, sword, archer, sword, archer, sword])
player = Player('Henryk')

print(shop)
print(player)

print()
shop.buy_item(archer)

print(player)
print(shop)

root = tk.Tk()
canvas = tk.Canvas(root, width="750", height="750", bg="brown")
button_sword = tk.Button(root, text="sword", command=lambda item=sword: shop.buy_item(item))
button1_window = canvas.create_window(10, 10, anchor=tk.NW, window=button_sword)
button_archer = tk.Button(root, text="archer", command=lambda item=archer: shop.buy_item(item))
button2_window = canvas.create_window(10, 30, anchor=tk.NW, window=button_archer)


goldvar = tk.IntVar()
goldvar.set(player.gold)
label = tk.Label(root, width=10)
label.configure(width=10, relief=tk.FLAT, textvariable=goldvar)
label_window = canvas.create_window(600, 10, anchor=tk.NW, window=label)
canvas.pack()
root.mainloop()

Upvotes: 1

Related Questions