简体   繁体   中英

How to change variable value from another class?

So I am working on a project which basically is to program a Monopoly. I want to be able for a player to pay X amount to the bank and have the bank properly receive and add that amount to its own. I am working the player on a Tkinter interface in a different class to the bank. I have done the following:

  • In Class Card1, import the Bank class from bank.py
  • Program a function pay_bank() where I get the amount entered in the Entry field and discount it from the player's available money and send it to bank. -Create a function receive_payment() in the Bank class where it receives the player's money and adds it to its own.

For some reason, it is not working so I need your help. Code below:

Class Card1:

def __init__(self):
        self.amount = 1500
        self.properties = {}
    
#nested functions to handle interface and it's events
    def manage_player1_card(self):

        def pay_bank():
            to_bank = int(payBox.get())
            if self.amount > to_bank:
                payBox.delete(0, END)
                self.amount -= to_bank
                Bank().receive_payment(to_bank)

class Bank:
    
    def __init__(self):
        self.bank_total = 14580

    def receive_payment(self, pay):
        self.bank_total += pay

*Indentation might look wrong due to copy-pasting but it is just fine in my code. What do you see I am doing wrong?

Basically, the self.bank_total amount is not adding up every time I enter the amount. For example: if I enter 500, it should go up to 15180, but it stays the same at 14580. I have debugged but it doesn't change. How can I fix this? Thanks!

Try something like this:

def pay_bank(bank: Bank):
    ...
    bank.receive_payment(to bank)

That way you are passing the bank as a parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM