0

I am currently writing a CFD program in python, the script uses a lot of predefined global variables in the calculations.

I would like to produce a GUI which allows the user to input all these variables, then runs the script and returns the results in the main console. I have tried using Tkinter to do this but can't seen to find a way to set global variables.

Below is a simple GUI I have tried designing to complete a much simpler calculation that also needs the global variables to be set.

'''GUI 2'''
import Tkinter
factor = 10

def GUIrun(n):
    return n * factor

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

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>",self.OnPressEnter)
        self.entryVariable.set("n")

        button=Tkinter.Button(self,text="Run",command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.labelVariable = Tkinter.StringVar()
        label=Tkinter.Label(self,textvariable=self.labelVariable,
        anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW')
        self.labelVariable.set("Hello!")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        n = self.entryVariable.get()
        self.labelVariable.set("n = " +self.entryVariable.get())
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)


    def OnButtonClick(self):
        n=self.entryVariable.get()
        return GUIrun(n)


if __name__=="__main__":
    app=simpleGUI(None)
    app.title('poster')
    app.initialize()
    app.mainloop()

I would appreciate any help Thank you

1
  • What is your issue? Why do you need global variables? If you do have a script using a lot of global values, I think you should rewrite and wrap them in a class. Commented Feb 7, 2014 at 17:21

1 Answer 1

1

Tkinter is no different than any other module with respect to global variables. Just declare a variable as global, then set it to some value.

Your code is working, though I'm not sure it's doing what you want it to do. If you want to do math, you need to convert the input value to an integer:

def OnButtonClick(self):
    n=int(self.entryVariable.get())
    ...

Your other option is to use an IntVar rather than a StringVar which will do the conversion for you.

The other problem seems to be a fundamental misunderstanding of how Tkinter works. Your OnButtonClick function correctly calls the function with the appropriate argument, then returns the result. Where do you think it returns it to? There's no code anywhere that is expecting a return value from a button command.

You need to do something with the result -- either print it out, pass it to another function, or set a variable. Calling return in a button callback is akin to throwing the result away.

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.