Getting inputs from comboboxes doesn't work [duplicate] - python

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

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 call an entry on tkinter? [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
I'm new to Python and I'm struggling to figure out what's wrong with my code.
import tkinter as tk
r = tk.Tk()
r.title('PROJECT')
def file_entry():
global ent
tk.Label(r, text = "ENTRY").grid(row = 2)
ent = tk.Entry(r).grid(row = 2, column = 1)
b_ent = tk.Button(r, text="OK", width = 5, command = print_input).grid(row = 2, column = 2)
def print_input():
print(ent.get())
l = tk.Label(r, text="OPTION 1 OR 2?").grid(row = 0)
b = tk.Button(r, text='1', width=25, command=file_entry).grid(row = 1,column = 0)
b2 = tk.Button(r, text='2', width=25, command=r.destroy).grid(row = 1,column = 1)
r.mainloop()
I'm trying to call "ent", but every time I run it, it would give me an error for it being a NoneType.
Like I said, I'm new to Python, and this is a school project, it would really be helpful if someone could explain what the problem is, as simply as possible.
Thanks!
It appears that chaining the grid to the entry widget is not acceptable.
If you put it on a separate line, the ent variable get properly assigned and get the inputted value as opposed to not working and being assigned None.
Try this:
def file_entry():
global ent
tk.Label(r, text="ENTRY").grid(row=2)
ent = tk.Entry(r)
ent.grid(row=2, column=1)
b_ent = tk.Button(r, text="OK", width=5, command=print_input).grid(row=2, column=2)

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

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