0

Ive tried reading as many articles about this as i can, but cannot find anything that helps me. In my code, i have created a class EntryWidget which has an entry widget amongst other things in it, and in my main class Controller for the app, i have created an instance of the class EntryWidget called self.entry. I am trying to write a method which will be a command for an ok button, which primarily takes the text from the entry widget and then manipulates it. this code is from the main class Controller:

     def ok(self):
         self.input = self.entry.get()
         self.command = ""

error:

     AttributeError: EntryWidget instance has no attribute 'get'

the idea is that text is entered, then the ok button is hit which calls this method. sorry if this has been asked before, but i have read the other responses and they dont help.

thanks

3 Answers 3

2

If your EntryWidget class doesn't subclass Tkinter.Entry, you will need to define get() method for your EntryWidget class. The error says that self.entry is a EntryWidget instance but does not contain get() method.

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

7 Comments

thankyou, youre absolutely right, it inherits from frame. how would i define the get method?
@user1710566 I am not sure what you are trying to do with the get method, but to simply define it in the EntryWidget class all you need to do is to put def get(self): # do something you want in your WidgetEntry class.
i would like to just get the text from the box as a string which i can assign to a variable and use. would something like work: def get(self): input = entry.Tkinter.Entry.get() return input
@user1710566 without knowing how you defined entry I am not sure if that will work. Can you paste the code?
@user1710566 it seems to me that self.entry.entrybox.get() is what you actually want instead of calling self.entry.get().
|
0

I wrote myself a small function that allows the user to enter a value into a tkinter.Entry, and then function returns the user's input.

def getInput(title, message):
    class inputGUI(tkinter.Tk):
        def __init__(self):
            tkinter.Tk.__init__(self)
            self.title(title)
            self.l=tkinter.Label(self,text=message, width = 30, pady = 10, padx = 5).pack()
            self.e=tkinter.Entry(self, width = 30)
            self.e.pack()
            self.e.focus()
            self.gap=tkinter.Label(self,text="", width = 50, pady = 0, padx = 5).pack()
            self.b=tkinter.Button(self,text='Submit',command=self.cleanup, width = 20, pady = 10, padx = 5).pack()
            self.gap=tkinter.Label(self,text="", width = 30, pady = 0, padx = 5).pack()
        def cleanup(self):
            self.userInput=self.e.get()
            self.destroy()
    root = inputGUI()
    root.focus()
    root.wait_window(root)
    valueOut = root.userInput
    return valueOut

Note: I'm aware there is no entry validation, however this should be easy enough to add.

Comments

0

Seems to me if your class subclassed Entry, you could use all methods from Entry. Such as .get(). Or define a tk StringVar() and then use it's .get() and .set() methods. You can think of an Entry Widget as a small tk Text widget so some things, such as delete( e.g. myEnt.delete(1.0,END) work the same.

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.