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.
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
.