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
Related
This question already has an answer here:
Tkinter IntVar returning PY_VAR0 instead of value
(1 answer)
Closed 5 months ago.
How to fix 'IntVar' object cannot be interpreted as an integer? I'm making a GUI password generator based on my old console generator. After pressing "Generate" button, i getting that error.
import random
import string
from tkinter import *
from tkinter import ttk
tkWindow = Tk()
tkWindow.geometry('250x150')
tkWindow.title('Password Generator')
characters = list(string.ascii_letters + string.digits + "!##$%^&*()")
def passgen():
random.shuffle(characters)
password = []
for i in range(length):
password.append(random.choice(characters))
random.shuffle(password)
print("".join(password))
lengthLabel = Label(tkWindow, text="Password length:").grid(row=0, column=0)
length = IntVar()
lengthEntry = Entry(tkWindow, textvariable=length).grid(row=0, column=1)
GenButton = Button(tkWindow, text="Generate", command=passgen).grid(row=1, column=0)
tkWindow.mainloop()
Use length.get() in for loop present in passgen() method instead of length only. Like this:
for i in range(length.get()):
password.append(random.choice(characters))
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
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!
This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
I am a very very very beginner with programming languages, and now I started learning python, for basic I have learned it and now I just started trying to make something like a simple calculator with a Tkinter. here I am trying to make a simple calculator with 'Entry' and 'Button', but I have difficulty with what to expect.
what I want in my application is: how to combine or add values from the first and second entry fields with the button in question (+), then after the button is clicked the results appear in the box entry next to the (+) button.
I know that there have been many questions here with similar questions (Entry widgets etc) but I am really a beginner and have not gotten any answers by looking at the questions that were already in this forum.
sorry for this beginner question, and I beg for your help.
import tkinter as tk
from tkinter import *
root = tk.Tk() #main windownya
root.geometry("300x300")
root.title("Little Calc")
#Label
Label(root, text="Input First Number: ").grid(row=0, column=0)
Label(root, text="Input Second Number: ").grid(row=1, column=0)
Label(root, text="Choose Addition: ").grid(row=2, column=0)
Label(root, text="Result: ").grid(row=2, column=1)
#Entry
firstnumb = Entry(textvariable=IntVar()).grid(row=0, column=1)
secnumb = Entry(textvariable=IntVar()).grid(row=1, column=1)
#Plus Button
plus_button = Button(root, text="+", command=lambda: firstnumb.get()+secnumb.get(), width=6, height=1).grid(row=3, column=0)
plusresult = Entry(plus_button).grid(row=3, column=1)
root.mainloop()
When I run your code, I get a nullPointerException here:
plus_button = Button(root, text="+", command=lambda: firstnumb.get()+secnumb.get(), width=6, height=1).grid(row=3, column=0)
Why does this happen? You use the get() method on firstnumb and secnumb, those variables are defines as:
firstnumb = Entry(textvariable=IntVar()).grid(row=0, column=1)
secnumb = Entry(textvariable=IntVar()).grid(row=1, column=1)
And both these variables have null as value instead of the Entry object that you would want. This happens because you also use the grid() method in your variable declaration and this method returns null. Doing this in two steps fixes your problem.
firstnumb = Entry(textvariable=IntVar())
firstnumb.grid(row=0, column=1)
secnumb = Entry(textvariable=IntVar())
secnumb.grid(row=1, column=1)
When I now click the "+" button no error occurs but the result is also not yet displayed. I advise you to make a new function where you put this code: firstnumb.get()+secnumb.get(), width=6, height=1).grid(row=3, column=0 and the code to display the result.
Also, note that get() will return a string, make sure you first cast it to an int or float before adding both values. If you don't do that, 10 + 15 will give 1015.
This question already has answers here:
Error when configuring tkinter widget: 'NoneType' object has no attribute
(2 answers)
Closed 6 years ago.
Lurked around the forums but can't seem to get the get() function to works, it keeps returning that it is not defined. Can somebody point out what I did wrong?
from Tkinter import *
the_window = Tk()
def button_pressed ():
content = entry.get()
if (content == '1'):
print 'lol'
else:
print 'boo'
entry = Entry(master=None, width = 8, bg='grey').grid(row=2, column = 2)
button = Button(master=None, height=1, width=6, text='Go!', command=button_pressed).grid(row=2, pady=5, column=3)
the_window.mainloop()
The grid method returns None. This assigns a None value to entry.
Instead, you want to assign that instance of Entry to entry and then modify the grid:
entry = Entry(master=None, width = 8, bg='grey')
entry.grid(row=2, column = 2)
entry = Entry(master=None, width = 8, bg='grey').grid(row=2, column = 2)
This will assign entry to the return value of the .grid() method, but .grid() does not return anything, so entry will be None.
You should instead write
entry = Entry(master=None, width=8, bg='grey')
entry.grid(row=2, column=2)
Do the same for all other widgets.