-1

I'm trying to get a value from an Entry box which is my input, and I want whatever is written in it to be available for processing on button press. Here is the code I have:

from tkinter import *
import tkinter as tk

class GUI(Tk):

    def __init__(self):
        super(GUI, self).__init__()

        tk.Label(self, text="Input").grid(row=0)
        self.input = tk.Entry(self).grid(row=0, column=1)

        tk.Button(self, text="Start", command=self.start_button).grid(row=5, column=1)

    def start_button(self):
        print(self.input.get())

gui = GUI()
gui.mainloop()

Whenever I try to press the button, it says None type has no attribute "get". Here is the traceback:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "D:/Projects/excel_reader/test.py", line 15, in start_button
    print(self.input.get())
AttributeError: 'NoneType' object has no attribute 'get'

I have tried removing the TK as subclass for my GUI class, removing the super() call and tried declaring all the variables globally but that did not work either. I am very new to Python GUI.

halfer
  • 19,824
  • 17
  • 99
  • 186
Naeem Khan
  • 950
  • 4
  • 13
  • 34

1 Answers1

0

The problem turns out to be the way I was defining the grids for it. I don't know why but this works now by changing this-> input = tk.Entry(root).grid(row=0, column=1)

to this:

global input
input = tk.Entry(self)
input.grid(row=0, column=1)
Naeem Khan
  • 950
  • 4
  • 13
  • 34
  • 1
    The reason is the `grid()` method doesn't return anything, so its return value is effectively `None`. This is a common tkinter beginner mistake. – martineau Oct 19 '19 at 17:05