1

I have a Tk App that has several entry boxes for number inputs:

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

This is fully working and I can use the number inputs in other functions. However, I want a separate function that fills those entry boxes with generic numbers specified by the function when a 'fill with generic numbers' button is clicked. Is this possible?

EDIT: I have a button for a generic simulation of those numbers mentioned but i'd like the entry boxes to display the numbers I specify.

Joey
  • 914
  • 4
  • 16
  • 37
  • 1
    Have you done any research? What you're asking is covered in lots of tutorials and tkinter documentation. You merely need to call the `set` method of the variables to whatever values you want. – Bryan Oakley Jan 22 '16 at 12:01
  • It's quite hard to research and specify what you want when you don't know the technical names e.g. set. But this worked, thanks. In future I will look at the documentation more rigorously. – Joey Jan 22 '16 at 12:22
  • You don't need to know the technical names, you just need to read the documentation of the classes you're instantiating. – Bryan Oakley Jan 22 '16 at 12:43
  • Also, FWIW, you can omit the use of `self.entry =` here. All you're doing is setting it to `None` over and over again. – Bryan Oakley Jan 22 '16 at 12:46
  • How else would I obtain my StringVar assigned to each Entry? – Joey Jan 22 '16 at 14:31
  • well, you can't do it now, so it would be no different. There are many, many questions and answers on stackoverflow related to the question of why `self.entry` (in your case) is always set to `None`. For example, http://stackoverflow.com/a/1101765/7432 – Bryan Oakley Jan 22 '16 at 14:34

1 Answers1

2

The contents of the Entry widget are directly linked to its control variable, so calling the .set() method on the corresponding StringVar will set the contents of the entry

self.c2_low.set("c2_low")
self.c2_high.set(23)   #gets converted to a str automatically

then to get the contents back use the .get() method on same variable:

try:
    x = int( self.c2_low.get() )
except ValueError:
    NotImplemented   #handle invalid input

Also you are calling the .grid() method on the newly created Entries which loses the reference to them later since .grid() returns None, it would probably be easier to use a simple class to group the textvariable together with the Entry widget:

class Smart_entry(Tkinter.Entry):
    def __init__(self,master,column,row,content="",name=None):
        self.textvar = Tkinter.StringVar(master,content,name)
        Tkinter.Entry.__init__(self,master,textvariable=self.textvar)
        self.grid(column=column, row=row, sticky="W")

    def get(self):
        return self.textvar.get()

    def set(self,value):
        return self.textvar.set(value)
 # if all the entries are int/float 
 # then handling the conversion in the .get() may be useful
##    def get(self,conv=str):
##        "returns content after converting to specified type, returns None if conversion failed"
##        try:
##            return conv( self.textvar.get() )
##        except ValueError:
##            return None
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59