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

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()

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

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!

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

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.

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

What should I be using instead of .get() [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 8 years ago.
Using python I wrote the code below. I am trying to make a small calculator which multiplies an input by 5. When using the .get() command, I get 'NoneType' object has no attribute 'get'.
Can anyone help?
from Tkinter import *
def calc_handler():
question = entry.get()
answer = question * 5
print answer
main = Tk()
main.title('Calculator')
main.geometry('350x100+300+100')
instructions = Label(main, text='Input A Number And I Will Multiply It By 5').grid(row=0, columnspan=2)
entry = Entry(main).grid(row=1, columnspan=2)
enter = Button(main, text='Click Here To Calculate', command=calc_handler).grid(row=3, column=1)
clear = Button(main, text='Clear').grid(row=3, column=2)
mainloop()
It is hard to define why it happens (I reckon grid return a None object) but change this line:
entry = Entry(main).grid(row=1, columnspan=2)
To:
entry = Entry(main)
entry.grid(row=1, columnspan=2)
And the reason why it never actually multiplies is that because question is a string thus you need to convert it to a integer by using the int() function before multiplying it:
def calc_handler():
question = entry.get()
answer = int(question) * 5
print answer

Categories

Resources