1

I apologize if this question has been asked before but i have been trying to solve this problem by myself and with the help of my classmates. They seem to have no problem but i just cant get to the Value of an Entry. I always get strange strings like <function StringVar.get at 0x0358E970> or similar. I have spent full 5 hours now trying to solve this with the help of the internet too. I hope there is someone out there who can help me understand this.

So I have this class with a function in which i create a Tkinter window with one simple entry and one simple button. But when i try to get the value of the textvariable i dont get a string but some kind of code i cant work with at all. What am i doing wrong? Or is there something i am not doing?

 import tkinter as tk from tkinter import ttk from tkinter import * class StrWnd: def __init__(self): self.firstname = StringVar def Register(self): self.register = Toplevel(self.scr) self.register.geometry("300x300") self.register.title("Register") Label(self.register, text="Vorname(Keine Nummern)").pack() ttk.Entry(self.register, textvariable=self.firstname).pack() ttk.Button(self.register, text="Submit", command=lambda: self.printValues()).pack() def printValues(self): print(self.firstname.get) 

Any tips are welcome.

5
  • 2
    it's .get(), not .get. Commented Jun 3, 2020 at 20:56
  • @BryanOakley I know that I have no brackets there but that is actually necessary. If I add brackets i get the error print(self.firstname.get()) TypeError: get() missing 1 required positional argument: 'self' Commented Jun 3, 2020 at 21:06
  • @BryanOakley if I also add self into the brackets i get the error value = self._tk.globalgetvar(self._name) AttributeError: 'StrWnd' object has no attribute '_tk' Commented Jun 3, 2020 at 21:08
  • 2
    StringVar also has to be StringVar(). Commented Jun 3, 2020 at 21:22
  • @BryanOakley then I get the error self._root = master._root() AttributeError: 'NoneType' object has no attribute '_root' Commented Jun 3, 2020 at 21:28

2 Answers 2

2

Ok, there are many problems in your code.

To answer your specific question "How to get text from Entry", you need to call the get method. The flaw in your code is that you aren't calling the get method. You need to change printValues to look like the following. Notice the use of () after get:

def printValues(self): print(self.firstname.get()) 

However, that exposes the next problem in your code. You're making the same type of mistake when you create self.firstname. You need to change it to the following. Again, notice the use of ().

self.firstname = StringVar() 

The third problem is that you can only do that after first creating the root window. Your example doesn't show if, how, or when you do that, but in the comments you reported an error that is a symptom of this type of mistake.

So, before you create an instance of StrWnd you must create the root window. For example, it might look like this:

root = tk.Tk() strwnd = StrWnd() 

Though, you seem to have another class that represents the root window so that's probably not exactly how you'll do it in your code. The point is, the root window needs to be created before you create an instance of StringVar.

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

3 Comments

well its working now i guess. But for some reason the print is just blank.
When I make those changes to your exact code, and add the other few bits to make it work, it works for me. Are you creating more than once instance of Tk?
It is working now. I had simply forgotten to add the root to the Toplevel. Thank you so much. Its already midnight now and I can finally go to sleep without breaking my head about this
1

I am fairly new to coding myself, but the first issue I spot is that it would have to be tk.StringVar() or StringVar() as this is not merely a variable but a predefined class in Tkinter.

To find out more, see this post: what is the difference between a variable and StringVar() of tkinter

Besides, I am not sure if all your functions are called correctly. In the last line, I would expect self.firstname.get() rather than self.firstname.get.

I am certain, however, that more experienced coders can give you much more helpful advice.

EDIT: Good to see that someone has just given you a much more detailed response already. :-)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.