-1

I am using Tkinter and I am having trouble with the get method. I saw that it was a common issue (here for example 'NoneType' object has no attribute 'get') but I don't really understand how to fix it.

I thought that station_I.get() was suppose to return a string variable but apparently I was wrong.

What is this issue due to ?

PS: I am using Tkinter with Python 2.7

Here is the error I get:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\python\Anaconda2\lib\lib-tk\Tkinter.py", line 1542, in __call__
    return self.func(*args)
  File "C:/Users/python/Documents/project.py", line 168, in path
    print station_I.get()
AttributeError: 'NoneType' object has no attribute 'get'

Here is my code:

from Tkinter import *
import ttk


def path():
    print station_I.get()

window = Tk()  

stations = ["1","2","3"]

text = StringVar()
text.set("Path: ")

station_I = ttk.Combobox(window, values = stations).place(x=50, y=50)
station_F = ttk.Combobox(window, values = stations).place(x=50, y=100)

bouton = Button(window, command = path, text = "Calculate").place(x=125,y=150)
label = Label(window, textvariable = text).place(x=50,y=225)

window.geometry("330x400")
window.mainloop()
Loïc Poncin
  • 511
  • 1
  • 11
  • 30

1 Answers1

1

make sure you position your geometry manager on the next line after your variable for your widget

Replace this

station_I = ttk.Combobox(window, values = stations).place(x=50, y=50)

with

station_I = ttk.Combobox(window, values = stations)
station_I.place(x=50, y=50)

and all the variable for the widget variable with the example above.

Full code

from tkinter import *
from  tkinter import ttk


def path():
    print (station_I.get())

window = Tk()

stations = ["1","2","3"]

text = StringVar()
text.set("Path: ")

station_I = ttk.Combobox(window, values = stations)
station_I.place(x=50, y=50)
station_F = ttk.Combobox(window, values = stations)
station_F.place(x=50, y=100)

bouton = Button(window, command = path, text = "Calculate")
bouton.place(x=125,y=150)
label = Label(window, textvariable = text)
label.place(x=50,y=225)

window.geometry("330x400")
window.mainloop()
AD WAN
  • 1,414
  • 2
  • 15
  • 28
  • Thank you, but why do we have to set the position after every time ? – Loïc Poncin Mar 26 '18 at 22:06
  • if not it will return as `Nonetype` try this and see print type of your variable for the widget and print mine also you see you will get `' and get `` for mine – AD WAN Mar 26 '18 at 22:20