Reputation: 29
I am trying to get input from an entry widget using Python Tkinter using Notebook. I have managed to get it to work on my main canvas but as soon as I introduced tabs, it would not work. I am clearly missing something linking the .get function to the correct location but I can't work out what. Any ideas?
import tkinter
import win32api
from tkinter import ttk
checklist = tkinter.Tk()
checklist.title("My Checklist")
ScreenWidth=checklist.winfo_screenwidth()
ScreenHeight=checklist.winfo_screenheight()
checklist.geometry("%dx%d+0+0" % (ScreenWidth, ScreenHeight))
count = 0
tasks = []
tabcontrol = ttk.Notebook(checklist, width = 500, height = 500)
tab = ttk.Frame(tabcontrol)
tabcontrol.add(tab, text = "Checklist Home Page")
tabcontrol.grid(row = 0, column = 0)
main_canvas = tkinter.Canvas(checklist, width = 1000, height = 1000, highlightbackground="green")
main_canvas.grid(row=1, column = 0)
main_canvas.columnconfigure(3)
main_canvas.rowconfigure(6)
def create_checklist():
global count
count += 1
if count > 10:
win32api.MessageBox(0, 'Maximum number of checklists made!')
else:
tab = ttk.Frame(checklist, width = 500, height = 500)
tabcontrol.add(tab, text = count)
tabcontrol.grid(row = 0, column = 0)
button_add_task = tkinter.Button(tab, text="Add task", width=20, height=1, bg="purple", command=add_task).grid(row = 2, column= 2, pady = (100, 1))
item_entry = tkinter.Entry(tab).grid(row=6, column =2)
list_items = tkinter.Listbox(tab)
list_items.grid(row =7, column =2, pady=10)
def add_task():
task = item_entry.get()
if task !="":
tasks.append('- ' + task)
update_listbox()
button_create_checklist = tkinter.Button(main_canvas, text="New Checklist", width=20, height=1, bg = "purple", command=create_checklist).grid(row = 3, column = 2, pady = 1 )
checklist.mainloop()
My error is currently:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\lucas\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\lucas\OneDrive\Documents\Employability\untitled0999py.py", line 40, in add_task
task = item_entry.get()
NameError: name 'item_entry' is not defined
Upvotes: 0
Views: 666
Reputation: 771
Since item_entry
is local variable of function create checklist()
, it can't be used in add_task()
function. So, we'll pass value of Entry
field whenever Add Task
button is pressed. To do so, we can use lambda expression in command
option of Button
. There are some minor changes in add_task()
function.
Remember: grid
is used to organize widgets and it returns None, so item_entry
will be a NoneType
object in your code and using get()
with it will raise AttributeError
.
So, we need to replace
item_entry = tkinter.Entry(tab).grid(row=6, column =2)
with
item_entry = tkinter.Entry(master=tab)
item_entry.grid(row=6, column =2)
This code works Fine:
def create_checklist():
global count
count += 1
if count > 10:
win32api.MessageBox(0, 'Maximum number of checklists made!')
else:
tab = ttk.Frame(checklist, width = 500, height = 500)
tabcontrol.add(tab, text = count)
tabcontrol.grid(row = 0, column = 0)
item_entry = tkinter.Entry(master=tab) # Changes in
item_entry.grid(row=6, column =2) # these lines
button_add_task = tkinter.Button(tab, text="Add task", width=20, height=1, bg="purple", command=lambda :add_task(item_entry.get())).grid(row = 2, column= 2, pady = (100, 1))
list_items = tkinter.Listbox(tab)
list_items.grid(row =7, column =2, pady=10)
def add_task(task):
if task !="":
tasks.append('- ' + task)
update_listbox()
Hope this helps.
Upvotes: 2