Okay, well I'm having trouble getting a variable from one place to the other. I have two scripts: for now, let's call them MainScript.py and ChildScript.py.

#!/usr/bin/python
# MainScript.py

import ChildScript
from Tkinter import *

class GetVariable:
   def __init__(self):
      master = Tk()
      master.withdraw()

      ChildScript.VariableDialog(master)
      
      # From right here, I would like to be able to print the value of 'buttonstatus'.
      
myapp = GetVariable()
#!/usr/bin/python
# ChildScript.py

import tkSimpleDialog
from Tkinter import *

class VariableDialog(tkSimpleDialog.Dialog):
   def body(self, master):
      self.status = IntVar()
      Checkbutton(master, text="Variable", variable=self.status).grid(row=1)
      
   def apply(self):
      buttonstatus = self.status.get()
      
      # Now, I have the status of the checkbutton locally.
      # How can I get it to the MainScript script?

However, I would like to do all of this without any globals. I resent them!

Thank you in advance.

Recommended Answers

All 3 Replies

There are three things in your code which require attention.
First, buttonstatus is lost as soon as VariableDialog.apply returns. To keep this value, simply return it:

def apply(self):
      buttonstatus = self.status.get()
      return buttonstatus

Then, to get the value you are interested in, call the apply method: status = [I]something[/I].apply() Final question - what is this something? It must be an instance of VariableDialog class (because it is where apply() is defined). Notice that you already created this instance at line 12 of MainScript. You just need to remember this created instance; then you may call its methods:

dialog = ChildScript.VariableDialog(master)
      # From right here, I would like to be able to print the value of 'buttonstatus'.
      status = dialog.apply()

Our fellow Pythonian nezachem is correct.

You could also have used the usual
if __name__ == '__main__':
to test your module code ...

#!/usr/bin/python
# ChildScript.py

import tkSimpleDialog
from Tkinter import *

class VariableDialog(tkSimpleDialog.Dialog):
    def body(self, master):
        self.status = IntVar()
        cb = Checkbutton(master, text="Variable", variable=self.status)
        cb.grid(row=1)
      
    def apply(self):
        buttonstatus = self.status.get()
        return buttonstatus


# test the module as standalone program
if __name__ == '__main__':
    master = Tk()
    master.withdraw()

    dlg = VariableDialog(master)
    bstatus = dlg.apply()
    print bstatus

Well, the solution was much simpler than I thought. After reading this page ("[apply] is called automatically to process the data, *after* the dialog is destroyed"), I did not know that you could simple call the apply method again via the class instance.

Thank you very much for the response. Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.