Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Player Creation

I previously made a game where I assisgned a Player Class to something meaning I could format stuff that i needed such as

print("HEy you're at {} health".format(PlayerIG.health))

Here my new code I included tkinter and I don't know if that's what messing up

But here it is :

import tkinter as tk

LARGE_FONT=("Verdana", 12)

class Main(tk.Tk):

   def __init__(self):
      tk.Tk.__init__(self)
      container = tk.Frame(self)
      container.pack(side="top", fill="both", expand = True)
      container.grid_rowconfigure(0, weight=1)
      container.grid_columnconfigure(0, weight=1)

      self.frames = {}
      for F in (StartPage, Mage, Warrior, Archer):
         frame = F(container, self, "Mage")
         self.frames[F] = frame
         frame.grid(row=0, column = 0, sticky="nsew")
      self.show_frame(StartPage)

   def show_frame(self, cont):
      frame = self.frames[cont]
      frame.tkraise()



class Mage(tk.Frame):
    def __init__(self, parent, controller, Mage):
        #Graphics
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Character : {}".format(Mage))
        label.pack(pady=10,padx=10)

        self.name = None # mage
        self.maxhealth = 100
        self.health = self.maxhealth
        self.pots = 5
        self.it = 0
MageIG = Mage("Mage")            
class Archer(tk.Frame):
    def __init__(self, parent, controller, Archer):
        #Graphics
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Archer Class")
        label.pack(pady=10,padx=10)

        self.name = None # mage
        self.maxhealth = 100
        self.health = self.maxhealth
        self.pots = 5
        self.it = 0
class Warrior(tk.Frame):
    def __init__(self, parent, controller, Warrior):
        #Graphics
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Warrior")
        label.pack(pady=10,padx=10)

        self.name = None # Warrior
        self.maxhealth = 100
        self.health = self.maxhealth
        self.pots = 5
        self.it = 0



class StartPage(tk.Frame):

   def __init__(self, parent, controller, *args):
      tk.Frame.__init__(self,parent)
      label = tk.Label(self, text="Select a class", font=LARGE_FONT)
      label.pack(pady=10,padx=10)

      button1 = tk.Button(self, text="Mage",
                          command=lambda: controller.show_frame(Mage))
      button1.pack()
      button2 = tk.Button(self, text="Warrior",
                          command=lambda: controller.show_frame(Warrior))
      button2.pack()
      button3 = tk.Button(self, text="Archer",
                          command=lambda: controller.show_frame(Archer))
      button3.pack()



app = Main()
app.mainloop()

I tried to assign PlayerIG to the Mage Class but it says that it's missing to positional arguments if anyone could try to run this code and tell me what's wrong it'd be of great help

Thanks again to the community :}

2 Answers

look at the init for your Mage class

def __init__(self, parent, controller, Mage):

your class needs three arguments to be instatiated that's why your MageIG = Mage("Mage") doesn't work.

There is one thing that you should improve on your architecture, your presentation layer (tkinter definitions) is too coupled with the logic layer (the definition of type of characters like Warrior and Mage).

Also you should look at inheritance concept, there is too much repetition on your classes.

It is not a complete answer but its a start.

So that would be like

MageIG = Mage(parent controller Mage)

?

Still thanks a lot of the answer and ill try to get it :P _}

This didn't work it adds an error saying that they are not defined :P