0

I'm trying to create a request form and return the values of each field for storage in a DB later.

I can't seem to get the values back from the entry fields using the .get method on them in my submit button?

#imported modules
from tkinter import *
from tkinter import ttk

#declared
window = Tk()
spares = IntVar()

#window config
window.title("Histology Further Work")
window.geometry('600x400')
window.configure(background = "light grey")

#combobox for option selection
comboEx = ttk.Combobox(window, values=[" ***Select*** ", "Option 1", "Option 2", "Option 3"])
comboEx.current(0)
comboEx.grid(row = 6, column = 1)
print(comboEx.current(), comboEx.get())

#return values
def click_me():
    print(spares.get())
    print(comboEx.get())
    print(b1.get())

#field labels
a1 = Label(window, text = "Cassette ID").grid(row = 0,column = 0)
a2 = Label(window, text = "Block ID").grid(row = 1,column = 0)
a3 = Label(window, text = "Surname").grid(row = 2,column = 0)
a4 = Label(window, text = "Test required").grid(row = 3,column = 0)
a5 = Label(window, text = "Spares on case?").grid(row = 4,column = 0)
a6 = Label(window, text = "Comments").grid(row = 5,column = 0)
a7 = Label(window, text = "Pathologist").grid(row = 6,column = 0)

#entry fields
b1 = Entry(window).grid(row = 0,column = 1)
b2 = Entry(window).grid(row = 1,column = 1)
b3 = Entry(window).grid(row = 2,column = 1)
b4 = Entry(window).grid(row = 3,column = 1)
b5 = Checkbutton(window, variable=spares).grid(row = 4,column = 1)
b6 = Entry(window).grid(row = 5,column = 1)


#submit button
btn = ttk.Button(window, text="Submit", command=click_me).grid(row = 7, column = 0)

window.mainloop()
Istoc
  • 11
  • 2
  • It is because those `Entry` widgets (b1, ..., b6) are all `None` as they are the result of `.grid(...)`. – acw1668 Mar 26 '21 at 10:08

1 Answers1

1

You should not say grid() on same line as class declaration because grid() returns None.

b1 = Entry(window)
b1.grid(row = 0,column = 1)

b2 = Entry(window)
b2.grid(row = 1,column = 1)

b3 = Entry(window)
b3.grid(row = 2,column = 1)

b4 = Entry(window)
b4.grid(row = 3,column = 1)

b5 = Checkbutton(window, variable=spares).grid(row = 4,column = 1)

b6 = Entry(window)
b6.grid(row = 5,column = 1)

The same rule applies to pack() and place() too. But why is b5 kept as it is? Because to get the value from b5 we use spares.get() and not b5.get().

Nihaal Nz
  • 51
  • 4