1

I know this question has been asked before but for the life of me I can't seem to understand the answers so I'm here asking for some help with an example. I am not trying to waste anybodies time here so please don't internet yell at me!

I have a class called Enemy with variables. How do I access these variables in another class? Heres my example.

import tkinter
from tkinter import ttk

class Enemy(object):
def __init__ (self, name, attack, defence, gold, health, experience):

    self.name = name
    self.attack = attack
    self.defence = defence
    self.gold = gold
    self.health = health
    self.experience = experience

    Enemy1 = Enemy("Enemy Soldier", "5", "1", "10", "10", "30")

class Application(object):

def __init__(self):

    self.enemyLabel = tkinter.Label(text= self.Enemy1.name)
    self.enemyLabel.pack()     

myApp = Application()
myApp.root.mainloop()

This is a very narrowed down version of my code.

2 Answers 2

1

You have to define enemy1 in the scope of Application.__init__:

class Enemy(object):
    def init (self, name, attack, defence, gold, health, experience):
        self.name = name
        self.attack = attack
        self.defence = defence
        self.gold = gold
        self.health = health
        self.experience = experience

class Application(object):
    def __init__(self):
        self.enemy1 = Enemy("Enemy Soldier", "5", "1", "10", "10", "30")
        self.enemyLabel = tkinter.Label(text=self.enemy1.name)
        self.enemyLabel.pack()    
Sign up to request clarification or add additional context in comments.

Comments

0

Anywhere within the scope of where you created Enemy1, you can simply use Enemy1.name, Enemy1.attack, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.