How do I use .get() in tkinter? [duplicate] - python

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 4 years ago.
from tkinter import *
main = Tk()
e = Entry(main).pack()
main.configure(bg = "Black")
s = e.get()
main.mainloop()
but then I get this error:
Traceback (most recent call last):
File "//agsb2/2014intake/kfullman14/My Documents/CICT/Programming U6/Python/Python Challenges/Tkinter test.py", line 9, in <module>
s = e.get()
AttributeError: 'NoneType' object has no attribute 'get'
Any help would be greatly appreciated. Thanks in advance.

Try this:
from tkinter import *
def button():
s = e.get()
label.config(text ='See? There is a difference.You entered %s.' %(s))
main = Tk()
e = Entry(main, bd=5, width=40)
main.configure(bg = "Black")
button = Button(main, text="Do Something", command=button)
label = Label(main, text="I am needed to show the difference.")
e.pack()
label.pack()
button.pack()
main.mainloop()
You did declare the variable but you never said how and when to use it. Hence, you did not see any change. In the example above you have a button that reads the Input and uses it to change a label.

Related

How can i get and save a value from an entry if i use grid?(tkinter) [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
How can i get a value from an entry if i use grid?
from tkinter import *
window = Tk()
window.title("Test")
window.geometry(600x600)
window.config()
e = Entry(window, width=20).grid(row=0, column=1)
entry = e.get()
print(entry)
Move the .grid() down on a separate line
entry = Entry(window, width=20)
entry.grid(row=0, column=1)
print(entry)
BTW, you should have searched this site. I am sure there are millions like this

Why can I not use the get attribute in tkinter? [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
from tkinter import *
root = Tk()
root.geometry("850x900")
def pri():
user = e.get()
print(user)
e = Entry(root).pack()
b = Button(root, text = "Submit", command=pri).pack()
root.mainloop()
When I run this code, I get the following error:
"AttributeError: 'NoneType' object has no attribute 'get'"
The problem is that you are trying to assign, to a variable, the result of a function that does not return anything (pack). You could first assign the Entry widget to the variable e and then pack. The following code works:
from tkinter import *
root = Tk()
root.geometry("850x900")
def pri():
user = e.get()
print(user)
e=Entry(root)
e.pack()
Button(root, text = "Submit", command=pri).pack()
root.mainloop()

Tkinter Entry .get from a definition [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
I keep getting a none Type attribute error and idk how to fix it as i looked on many videos and none helped. i don't know what I'm doing wrong. any help is appreciated!
from tkinter import *
def find():
user = whiteBox.get()
print(user)
root = Tk()
weatherResult = StringVar()
weatherResult.set("Enter a Place")
weather = Label(root, textvariable=weatherResult).pack()
whiteBox = Entry(root).pack()
check = Button(root, text="find", command=find).pack()
root.mainloop()
You are making a very common mistake. The value of your widget will be a NoneType, because you are using .pack on the same line.
So, your code:
from tkinter import *
def find():
user = whiteBox.get()
print(user)
root = Tk()
weatherResult = StringVar()
weatherResult.set("Enter a Place")
weather = Label(root, textvariable=weatherResult).pack()
whiteBox = Entry(root)
whiteBox.pack()
check = Button(root, text="find", command=find).pack()
root.mainloop()
This should be the result you want. Hope this helps!

'NoneType' object has no attribute 'get' in python little program [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
This code runs but crashes when I press the button it creates. It's my first post so If you have tips or If you need more info please comment.
The program should save the variable of the boxes in global variable. But when I try to press button occur error.
from tkinter import *
finestra1 = Tk()
finestra1.title("Prima Finestra")
testo1 = Label(finestra1, text ="Inserire modello infissi").grid(row=0, column=0)
spazioinput1 = Entry(finestra1, width=10, borderwidth=5).grid(row=0, column=1)
testo2= Label(finestra1, text ="Inserire numero finestre").grid(row=1, column=0)
spazioinput2 = Entry(finestra1, width=10, borderwidth=5).grid(row=1, column=1)
testo3= Label(finestra1, text ="Inserire numero balconi").grid(row=2, column=0)
spazioinput3 = Entry(finestra1, width=10, borderwidth=5).grid(row=2, column=1)
def primobottone():
# global modelloinfissi
global numerofinestre
global numerobalconi
modelloinfissi = spazioinput1.get()
numerofinestre = int(spazioinput3.get())
numerobalconi = int(spazioinput2.get())
Label(finestra1, text="Modello: " +modelloinfissi +" \nnumero: "+ numerobalconi+numerofinestre)
bottone1 = Button(finestra1, text= "Avanti", command = primobottone).grid(row=3)
finestra1.mainloop()
The error I hit:
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/salvatorefroncillo/Desktop/progetto/progetto.py", line 18, in primobottone
modelloinfissi = spazioinput1.get()
AttributeError: 'NoneType' object has no attribute 'get'
Entry.grid() returns None, so after this line:
spazioinput1 = Entry(finestra1, width=10, borderwidth=5).grid(row=0, column=1)
your spazioinput1 is indeed None. You need to split this in two statements:
spazioinput1 = Entry(finestra1, width=10, borderwidth=5)
spazioinput1.grid(row=0, column=1)
And do the same for all your widgets of course...

Tkinter Entry Field Capture AttributeError: [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 5 years ago.
I want to capture the data in the entry field from the second window defined under output. When I click submit get the following message: AttributeError: 'NoneType object has no attribute 'get'.
I feel like this should be an easy fix and do not understand why can't I capture the data from the entry field?
from tkinter import *
import xlsxwriter
class MyFirstGUI:
def __init__ (self, master):
master.title("Main")
master.geometry("400x400+230+160")
button1 = Button(master, text="1", command= self.output).grid(row=0, column=0)
def output(self):
cw1= Toplevel(root)
cw1.title("cw1")
cw1.geometry("400x300+160+160")
self.b2 = Button(cw1, text="Submit",command = self.write_to_xlsx).grid(row=0, column=2)
self.l2 = Label(cw1, text="New Specimen").grid(row=0, column=0)
self.e2 = Entry(cw1).grid(row=0, column=1)
def write_to_xlsx(self):
workbook = xlsxwriter.Workbook('tkintertest19.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_string('C1', self.e2.get())
workbook.close()
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
What you need to do is split the line
self.l2 = Label(cw1, text="New Specimen").grid(row=0, column=0)
into
self.l2 = Label(cw1, text = "New Specimen")
self.l2.grid(row=0, column=0)
Non-intuitive as this may seem, the grid/pack/place functions return None, so the whole shebang (Label().grid()) returns None. The solution is simply to split it up so that you get the right thing when you use .get().

Categories

Resources