1

I want to build a data entry application, but the entry boxes do not get the typed value. I know the get() method, but it does not work for some reason.. Here is the relevant code detail:

from tkinter import *

root = Tk()
root.geometry("600x500")

myList = []

class oneClass:
    def __init__(self, master):
        self.entryName = Entry(master).get()
        self.entryName.place(relx=0.5, rely=0.45, anchor=CENTER)
        myList.append(self.entryName)
        self.buttonPrint = Button(master, text="Click Me!", command=self.print).place(relx=0.5, rely=0.75, anchor=CENTER)

    def print(self):
        print(myList)

val = oneClass(root)

root.mainloop()

After running I got "AttributeError: 'str' object has no attribute 'place'" message in line 11. So what's the problem?

Nirevezs
  • 71
  • 8
  • That isn't how you use the `.get` method. What exactly are you trying to create? – TheLizzard Mar 07 '21 at 11:09
  • 2
    you are trying to get the entry as soon as you have created the entry widget. you need to wait for the user to enter the input. `.get()` method returns string and string as no place. – JacksonPro Mar 07 '21 at 11:09

1 Answers1

1

Try this:

from tkinter import *

# Now it will have global scope
myList = []

class oneClass:
    def __init__(self, master):
        self.entryName = Entry(master)
        self.entryName.place(relx=0.5, rely=0.45, anchor=CENTER)
        self.buttonPrint = Button(master, text="Click Me!", command=self.print)
        self.buttonPrint.place(relx=0.5, rely=0.75, anchor=CENTER)

    def print(self):
        myList.append(self.entryName.get())
        print(myList)

root = Tk()
app = oneClass(root)
root.mainloop()

You need to give time for the user to actually write their response in the entry before calling .get. I made the myList variable global. Also never use variable = Widget(...).function(...), when the function is a geometry manager function, because the variable will always be None for an explanation look here.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • maybe you want to change `variable = Widget(...).function(...)` -> `variable = Widget(...).place()`. It's not necessary that it becomes `None` it usually depends on what the method returns. – JacksonPro Mar 07 '21 at 11:36
  • @JacksonPro No it will still be `None`. All of the geometry methods like `.place`, `.pack`, and `.grid` return `None`. I can't think of any method on a tkinter widget that will return itself. So never use `variable = Widget(...).function(...)` just split it in 2 lines like this: `variable = Widget(...)` and `variable.function(...)` – TheLizzard Mar 07 '21 at 11:40
  • `Entry(master).get()` returns empty string. If you are talking only about the geometry manager that tkinter provides then your statement is true. – JacksonPro Mar 07 '21 at 11:41
  • Yes but OP used `self.entryName = Entry(master).get()` thinking it returned a widget. So always split it in 2 lines like this: `self.entryName = Entry(master)` and `entry_result = self.entryName.get()`. Also right now, I am ignoring the fact that you are querying the entry as soon as its created. Also just as a side note in The Zen of Python it says *Simple is better than complex* – TheLizzard Mar 07 '21 at 11:46
  • It works only with two line but at least it works. But actually that's pretty enough to me. Thank you for your helping! – Nirevezs Mar 07 '21 at 11:48
  • I was only commenting on this "Also never use variable = Widget(...).function(...) because the variable will always be None for an explanation" – JacksonPro Mar 07 '21 at 11:50
  • 1
    @JacksonPro I edited my explanation. I am not the best at explaining things – TheLizzard Mar 07 '21 at 11:52