0

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!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

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)
DaveStSomeWhere
  • 2,475
  • 2
  • 22
  • 19