0

I'm trying to carry a change from a global variable over to another module in Python2.7. I've done this in similar situations before but for some reason it won't work in this instance. The first file is the one that runs the program. It sets up a global variable and changes it according to the option selected. I've pasted a bit of the code below. runnerMod:

import Tkinter
from main_mod import*

choice = "0"

def main():
    main_mod=functOne()

class GUI(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
            self.initialize()

    def initialize(self):
        self.grid()
        self.update()
        btnOption1 = Tkinter.Button(self, text=u"Option 1", command=self.onButtonClick)
        btnOption1.grid(column=0, row=1)

    def onButtonClick(self):
        selection = "1"
        self.destroy()
        exec 'choice=%s' %selection in globals()
        main()

class menuSelection:
    def OPTIONCHOSEN(self):
        return choice

if __name == "__main__":
    app = GUI(None)
    app.mainloop

I want the global variable named choice from runnerMod.py to carry over to this module. main_mod:

from runnerMod import menuSelection

def functOne():
    userInput = menuSelection().OPTIONCHOSEN()
    print userInput

The global variable choice starts at 0, but I want to change it to 1 in the runnerMod.py module and have this reflected in the main_mod.py module. Since I'm rewriting an interface to an existing program my options are a little limited in the way its coded. Anyone have any ideas here?

5
  • 2
    Why are you modifying the choice with eval? Why not simply global choice and then choice = selection? I ask because menuSelection should work fine, so I'm guessing the value is not really changing. Commented May 29, 2012 at 9:07
  • As far as I can tell it's the same result. I'm just using eval since it's consistent with code elsewhere in the program. Either way it changes the global variable but doesn't carry that change over to the other module Commented May 29, 2012 at 9:15
  • Then I'm unsure what is wrong. I am able to modify and carry the variable between two modules with your code. My python is 2.7.3, not sure if it matters. Commented May 29, 2012 at 9:30
  • 1
    That's quite strange. As I stated I do have very similar code working elsewhere in the program but for some reason this particular variable will not send across. And it's also vital to the program I'm appending this too. I suppose it is possible that there is something else in the code interfering with this. Commented May 29, 2012 at 9:39
  • 1
    Consistency is good, but not good enough to justify new code using the evil exec and eval. Commented May 29, 2012 at 9:43

1 Answer 1

1

As it turns out, I couldn't pass over changes to a global variable from runnerMod.py because it was the module that launched the program. What I had to do was use runnerMod.py to launch the program and then call a function in main_mod.py. This function called BACK to a class in runnerMod.py and loaded up the GUI. Only by calling back and THEN modifying the global variable could I pass over the changes.

Sign up to request clarification or add additional context in comments.

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.