0

I am using a solution found to try and share variables throughout my code, which consists of 'Frame' classes. However, any attempt I make to try and change the value of these shared variables seems to have no effect, and after I attempt to change them, if I print it just returns blank. Any help would be appreciated.

class GolfApp(tk.Tk):
    def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    self.shared_data = {
        "currentcourse": tk.StringVar(),
        "numberofteams": tk.IntVar()}

    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 (MainMenu, CreatePage, ViewPage, GetTeamsPage, ChooseCourse, 
              AddCourse, LoginSignUp, Login, SignUp, Highscores1, Highscores2,
              Scorecards1, Scorecards2):
        page_name = F.__name__
        frame = F(parent=container, controller=self)
        self.frames[page_name] = frame
        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame("LoginSignUp")

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

This is where the solution from the link can be found. I have two variables, 'currentcourse' and 'numberofteams' which I need to share from one frame to others. I am attempting to set these variables in two different classes in the following bits of code.

class GetTeamsPage(tk.Frame):
    def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    self.configure(background="lightgreen")

    def set_teamnumber():
        numberofteams = answerentry.get()            
        numberofteams = self.controller.shared_data["numberofteams"].get()

    def testInt(inStr, i, acttyp):
        ind = int(i)
        if acttyp == '1':
           if not inStr[ind].isdigit():
               return False
        return True

    for col in range(7):
        self.grid_columnconfigure(col)


    for row in range(5):
        self.grid_rowconfigure(row)

    questionlbl = tk.Label(self,
                           text="How many teams/players are there?",
                           bg="lightgreen",
                           font = "Verdana 20 bold")

    questionlbl.grid(column=2,
                     row=0,
                     columnspan=3)

    answerentry = tk.Entry(self,
                           text="Enter a number here.",
                           validate = "key",
                           textvariable=self.controller.shared_data["numberofteams"])

    answerentry.grid(column=2,
                     row=2,
                     columnspan=3)

    moveonbtn = tk.Button(self,
                          text="Continue",
                          height = "3",
                          width = "40",
                          bg="darkgreen",
                          fg="lightgreen",
                          command = lambda: (controller.show_frame("CreatePage"), set_teamnumber()))

    moveonbtn.grid(column=1,
                   row=5,
                   columnspan=3)

    returnbtn = tk.Button(self,
                          height="3",
                          width="40",
                          bg="darkgreen",
                          fg="lightgreen",
                          text="Return to main menu",
                          command = lambda: controller.show_frame("MainMenu"))

    returnbtn.grid(column=4,
                   row=5,
                   columnspan=3)

class ChooseCourse(tk.Frame):
    def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    self.configure(background="lightgreen")
    cursor.execute("SELECT CourseName FROM Course")
    coursetuple = cursor.fetchall()
    courselist = [row[0] for row in coursetuple]


    def get_choice():
        currentcourse = self.controller.shared_data["currentcourse"]
        currentcourse = listmenu.get()


    for col in range(2):
        self.grid_columnconfigure(col, minsize=50)

    for row in range(7):
        self.grid_rowconfigure(row, minsize=60)

    titlelbl = tk.Label(self,
                        text="Choose a course",
                        bg="lightgreen",
                        font = "Verdana 20 bold")

    titlelbl.grid(column=2,
                  row=0)

    addbtn = tk.Button(self,
                       text="Add a new course",
                       bg="darkgreen",
                       fg="lightgreen",
                       command = lambda: controller.show_frame("AddCourse"))

    addbtn.grid(column=2,
                row=3)

    continuebtn = tk.Button(self,
                            text="Continue",
                            bg="darkgreen",
                            fg="lightgreen",
                            command = lambda: (controller.show_frame("GetTeamsPage"), get_choice))

    continuebtn.grid(column=2,
                     row=4)

    returnbtn = tk.Button(self,
                          text="Return to main menu",
                          bg="darkgreen",
                          fg="lightgreen",
                          command = lambda: controller.show_frame("MainMenu"))

    returnbtn.grid(column=2,
                   row=5)


    listmenu = tk.Listbox(self)

    for x in range(0, len(courselist)):     
            listmenu.insert("end", courselist[x])

    listmenu.grid(column=2,
                     row=1)
Community
  • 1
  • 1
tdbridger
  • 59
  • 1
  • 6
  • If you use the StringVar as a textvariable argument (as opposed to a text argument) then the value is set automatically; you dont need the set_teamnumber method. I don't feel like wading into your code any further; please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) if you want more help. – Novel May 03 '17 at 22:08

1 Answers1

1

You start by setting shared_data["current_course"] to an instance of StringVar, but then later you're resetting it to just a string.

Since it is a StringVar, you need to call the set method to set the value:

currentcourse = self.controller.shared_data["currentcourse"]
currentcourse.set(listmenu.get())
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685