Jguillot
Jguillot

Reputation: 214

problems with entry.get() in a class with Tkinter

I'm working on a GUI and i wanted to link it to my program, but I've many issues with get().

from Tkinter import *
class ProgramGui(Tk):

        gui = Tk()

        gui.grid()     

#Create Text  

        t = Label(gui,text='Enter your text : ')

        t.grid(column=1,row=1,sticky='EW')

#Create an entry

        e = Entry(gui)

        e.grid(column=2,row=1,sticky='EW')

        e.focus_set()

        def valueGET():

                print e.get()   

#Create button

        b=Button(gui, text="get", width=10, command=valueGET)

        b.grid(column=3,row=1,sticky='EW')

        mainloop()

This code function if i don't create a class, but i wanted to use this gui for my program so i need to put it in a class to call it after.

I get this error when i try my program :

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Anaconda\lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
  File "C:/Users/me/Desktop/my1rstGUI.py", line 24, in valueGET
    print e.get()
  File "C:\Anaconda\lib\lib-tk\Tkinter.py", line 2472, in get
    return self.tk.call(self._w, 'get')
TclError: invalid command name ".315386248L"
Exception in Tkinter callback

Upvotes: 0

Views: 204

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

For one, you need to move most of your code inside the constructor for the class:

class ProgramGui(Tk):
    def __init__(self):
        gui = Tk() 
        ....

Second, you need to save the references to the widgets you want to interact with:

self.e = Entry(gui)
...
self.e.get(...)

Completely unrelated to your question, gui.grid() does absolutely nothing. You can remove it.

Upvotes: 1

Related Questions