Tkinter Entry .get from a definition [duplicate] - python

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!

Related

TypeError: 'IntVar' object cannot be interpreted as an integer [duplicate]

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

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