0

I am making a game with levels and in each level, I will need to be using different operators and/or different ranges. My problem is that I don't know how to change the variables in a function from a different class. I would like to do this so I don't need to copy and paste my code making it lengthy. I'd like to use self.Answer and self.strQuestion for mulitple scope.

The code below is just to make the classes functional.

from tkinter import *
import tkinter as tk
import random
from Tkinter import messagebox

class BattleMaths(tk.Tk):
     def __init__(self, *args, **kwargs):        
          tk.Tk.__init__(self, *args, **kwargs)
          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, levelone, leveltwo):
               frame = F(container, self)
               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 StartPage(tk.Frame):
     def __init__(self, parent, controller):
          tk.Frame.__init__(self, parent)

          lvl1_button = Button(self, text="LEVEL 1", command=lambda: controller.show_frame(levelone))
          lvl1_button.place(relx=0.5, rely=0.5, anchor='center') 

I want to put the questions def into class leveltwo while changing it to self.Answer = int(numOne) * int(numTwo) and self.strQuestion = "{} x {}".format(str(numOne), str(numTwo))

class levelone(tk.Frame):
     def __init__(self, parent, controller):
          tk.Frame.__init__(self, parent)

    def widgets(self):
          #widgets here

    def question(self): 

          self.UserAnswer = ''
          numOne = random.randrange(1,10)
          numTwo = random.randrange(1,10)
          self.Answer = int(numOne) + int(numTwo) #change this
          self.strQuestion = "{} + {}".format(str(numOne), str(numTwo)) #and change this

    def answer(self):
          #answer checker

class leveltwo(tk.Frame):
     def __init__(self, parent, controller):
          tk.Frame.__init__(self, parent)

     #question def here

root = BattleMaths()
root.title("Battle Maths")
root.geometry("400x250")
root.resizable(0,0)
root.mainloop()
sdt
  • 21
  • 2
  • can you clearly explain, which is the variable that you want to use in multiple scopes? – rawwar May 10 '18 at 13:02
  • 1
    I recommend you start by reading all of the answers linked to in this answer: https://stackoverflow.com/a/7557028/7432 – Bryan Oakley May 10 '18 at 13:07

1 Answers1

0

Create the variables you want in the main class (BattleMaths), then you can alter them in the child classes via controller.my_variable.

Example: self.Answer created in BattleMaths and accessed in levelone via controller.Answer

10SecTom
  • 2,484
  • 4
  • 22
  • 26