0

I am trying to link entry variables to a function within Tkinter. I have 16 entry / variables that I want to use in my function. However, I'm struggling with the interface between the entry and the assigning of the variable.

my code:

import Tkinter 
import pandas as pd
import numpy as np

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

    def initialize(self):
        self.grid()

        c2_low =Tkinter.StringVar()
        c3_low =Tkinter.StringVar()
        ic4_low =Tkinter.StringVar()
        nc4_low =Tkinter.StringVar()
        ic5_low =Tkinter.StringVar()
        nc5_low =Tkinter.StringVar()
        neoc5_low =Tkinter.StringVar()
        n2_low = Tkinter.StringVar()

        c2_high =Tkinter.StringVar()
        c3_high =Tkinter.StringVar()
        ic4_high =Tkinter.StringVar()
        nc4_high =Tkinter.StringVar()
        ic5_high =Tkinter.StringVar()
        nc5_high =Tkinter.StringVar()
        neoc5_high=Tkinter.StringVar()
        n2_high = Tkinter.StringVar()


        self.entry = Tkinter.Entry(self, textvariable = c2_low).grid(column=0,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = c2_high).grid(column=0,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = c3_low).grid(column=0,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = c3_high).grid(column=0,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = ic4_low).grid(column=1,row=1,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = ic4_high).grid(column=1,row=2,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = nc4_low).grid(column=1,row=3,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = nc4_high).grid(column=1,row=4,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = ic5_low).grid(column=0,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = ic5_high).grid(column=0,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = nc5_low).grid(column=0,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = nc5_high).grid(column=0,row=8,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = neoc5_low).grid(column=1,row=5,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = neoc5_high).grid(column=1,row=6,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = n2_low).grid(column=1,row=7,sticky='EW')
        self.entry = Tkinter.Entry(self, textvariable = n2_high).grid(column=1,row=8,sticky='EW')
        self.resizable(False,False)

        self.resizable(False,False)

        button = Tkinter.Button(self,text=u"simulate")
        button.bind("<Button-1>", simulation)
        button.grid(column=3,row=9)

    def simulation(self):

        #code for this not included but uses all 16 entries/variable

if __name__ == "__main__":
    app = simulation_tk(None)
    app.title('Simulation')
    app.mainloop()

I want the input of my entry to be assigned to a variable which can then be used in a separate function (simulation)

4
  • assign each of em to self. Like self.c_low, self.c2_high etc. This way the other functions in this class will inherit them with "self" Commented Dec 11, 2015 at 14:25
  • If you name all entries differently(self.entry1, self.entry2 etc..), probably you won't be needing those variables anyway (ofc, you'll be needing to move grids to another line aswell). You can get contents of Entry by calling self.entry1.get() Commented Dec 11, 2015 at 14:34
  • You get the text from en entry box by referring to its textvariable by textvariable.get(), not to the Entry itself. Commented Dec 11, 2015 at 14:45
  • You are not making sense. You have to assign textvariable to the variable that has a Tkinter.StringVar() .... But I can't seem to use this variable in a separate function simulation() Commented Dec 11, 2015 at 14:49

2 Answers 2

1

You can get out the values from a box by calling its "textvariable" like

c2_low.get()

So if you change those variable to self.c2_low, self.c2_high etc. you will be able to call them inside your simulation function like:

import Tkinter
import pandas as pd
import numpy as np

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

    def initialize(self):
        self.c2_low =Tkinter.StringVar()
        self.entry = Tkinter.Entry(self, textvariable = self.c2_low).grid(column=0,row=1,sticky='EW')
        self.resizable(False,False)

        button = Tkinter.Button(self,text=u"simulate",command=self.simulation)
        button.grid(column=3,row=9)

    def simulation(self):
        print self.c2_low.get()
        #code for this not included but uses all 16 entries/variable

if __name__ == "__main__":
    app = simulation_tk(None)
    app.title('Simulation')
    app.mainloop()

Note that i changed the button as well, but now if you write something inside the entry, and press the button it will be printed to the console. From here you should be able to pass it into a variable or do anything with it.

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

3 Comments

For some reason, it's not printing anything =/
I am using python 2.7 with latest Tkinter, and it works. If i type "asd" inside the entry and press the button it ll appear inside the console.
Ah yes, indentation problem. Many thanks for your help!
0

My advice is to not use StringVar. It's simply not necessary, and is apparently adding to your confusion. This requires you restructure your code slightly, but the restructuring will make your code easier to maintain.

The first step is to separate widget creation from widget layout, and save a reference to each widget. In essence, we're remembering the widget rather than remembering the variable.

def initialize(self):
    self.grid()

    self.c2_low = Tkinter.Entry(self)
    self.c2_high = Tkinter.Entry(self)
    ...
    self.cd_low.grid(column=0,row=1,sticky='EW')
    self.c2_high.grid(column=0,row=2,sticky='EW')
    ...
    self.resizable(False,False)

Next, simply fetch the values from each entry widget in simulation:

def simulation(self):
    c2_low = self.c2_low.get()
    c2_high = self.c2_high.get()
    ...

Notice that this last part is the same whether you're using StringVars or not. In either case, you use the .get() method to fetch the value. It's just that with this method, you only have one object (the widget) rather than two (widget and variable) that you have to manage.

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.