Tkinter Entry Field Capture AttributeError: [duplicate] - python

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

Related

Why won't text in tkinter change? [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
I am having a trouble changing text in python even though though that I saw a lot of use it
My code:
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text).pack()
window.mainloop()
You assigned the return value of .pack() to btn, and pack doesn't return the widget it was called on (it returns None implicitly, since it has no useful return value). Just split up the creation of the button from the packing:
from tkinter import *
window = Tk()
def change_text():
btn.config(text="second text")
btn = Button(window, text="first text", command=change_text)
btn.pack()
window.mainloop()
your code is good but it is not good to make .pack() in the same line
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text )
btn.pack()
window.mainloop()
it works just will

Getting inputs from comboboxes doesn't work [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
I made a couple of combo-boxes, where the User can choose between pre-defined settings:
# create comboboxes for settings
combobox_sensortype = ttk.Combobox(root, values=('RGB','MS'), state='readonly').grid(row=3,column=2)
combobox_dem_dop = ttk.Combobox(root, values=('DEM','DOP'), state='readonly').grid(row=4,column=2)
combobox_sensortype_def = ttk.Combobox(root, values=('RGB_HZ','RGB_MR','MS_HZ','MS_MR'), state='readonly').grid(row=5,column=2)
combobox_run_SAGA_bat = ttk.Combobox(root, values=('Yes','No'), state='readonly').grid(row=6,column=2)
# getters for extracting user input from comboboxes
sensortype = combobox_sensortype.get()
demdop = combobox_dem_dop.get()
sensortype_def = combobox_sensortype_def.get()
run_SAGA_bat = combobox_run_SAGA_bat.get()
Unfortunately, I get this:
AttributeError: 'NoneType' object has no attribute 'get'
Whats the issue here?
I really hope someone can help.
This is because you called the method .grid(row=3,column=2). This doesn't return anything, thus the variable's value was set to None. To get your result, put them in separate lines. So you should define the Combobox, then grid it.
Solution:
import tkinter.ttk as ttk
from tkinter import *
root = Tk()
# create comboboxes for settings
combobox_sensortype = ttk.Combobox(root, values=('RGB', 'MS'), state='readonly') # define the combobox
combobox_sensortype.grid(row=3, column=2) # grid it!
combobox_dem_dop = ttk.Combobox(root, values=('DEM', 'DOP'), state='readonly')
combobox_dem_dop.grid(row=4, column=2)
combobox_sensortype_def = ttk.Combobox(root, values=('RGB_HZ', 'RGB_MR', 'MS_HZ', 'MS_MR'), state='readonly')
combobox_sensortype_def.grid(row=5, column=2)
combobox_run_SAGA_bat = ttk.Combobox(root, values=('Yes', 'No'), state='readonly')
combobox_run_SAGA_bat.grid(row=6, column=2)
# getters for extracting user input from comboboxes
sensortype = combobox_sensortype.get()
demdop = combobox_dem_dop.get()
sensortype_def = combobox_sensortype_def.get()
run_SAGA_bat = combobox_run_SAGA_bat.get()
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!

How to use get() method in Tkinter? [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
I've been trying to get a very simple text entry box and button going, but the following code creates an AttributeError when trying to get the value from self.entry, which is apparently a NoneType.
from tkinter import *
class Screen1(Frame):
def __init__(self, master):
super().__init__(master)
self.grid()
self.entry = Entry(self).grid(row=0, column=0)
self.bttn = Button(self,
text="Enter",
command=self.get_entry
).grid(row=0, column=1)
def get_entry(self):
message = self.entry.get()
print(message)
root = Tk()
root.geometry("400x100")
Screen1(root)
root.mainloop()
How do I fix this? Thanks
grid method returns None and you are setting that equal to self.entry
Change the following:
self.entry = Entry(self).grid(row=0, column=0)
# to
self.entry = Entry(self)
self.entry.grid(row=0, column=0)

Python can't get the .get function to work [duplicate]

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.

Categories

Resources