NameError: name 'tk' is not defined repl.it

Viewed 280

hey beginner programmer here, making a to do list for a high school assignment and cant figure out this error. its also my first time using python as I usually use java and JavaScript for projects. Im trying to add a dark and light mode to my program. Am I doing this correctly? btw, i'm using the online ide repl.it

here's my code for reference

import tkinter
import tkinter.messagebox
from ttkthemes import ThemedStyle
import tkinter.ttk as ttk 
import pickle

root = tkinter.Tk()
root.title("To-Do List")

def add_task():
    task = entry_task.get()
    if task != "":
        listbox_tasks.insert(tkinter.END, task)
        entry_task.delete(0, tkinter.END)
    else:
        tkinter.messagebox.showwarning(title="Warning", message="Please enter a task")

def delete_task():
     try:
         task_index = listbox_tasks.curselection()[0]
         listbox_tasks.delete(task_index)
     except:
         tkinter.messagebox.showwarning(title="Warning", message="Please select a task first")

 def load_tasks():
     try:
         tasks = pickle.load(open("tasks.dat", "rb"))
         listbox_tasks.delete(0, tkinter.END)
         for task in tasks:
             listbox_tasks.insert(tkinter.END, task)
     except:
         tkinter.messagebox.showwarning(title="Warning", message="Cant find saved task file")

 def save_tasks():
     tasks = listbox_tasks.get(0, listbox_tasks.size())
     pickle.dump(tasks, open("tasks.dat", "wb"))

 # Dark and light modes   
 app = tk.Tk()
 app.geometry("200x400")
 app.title("Changing Themes")
 # Setting Theme
 style = ThemedStyle(app)
 style.set_theme("scidgrey")

 # Button Widgets
 Def_Btn = tk.Button(app,text='Default Button')
 Def_Btn.pack()
 Themed_Btn = ttk.Button(app,text='Themed button')
 Themed_Btn.pack()

 # Scrollbar Widgets
 Def_Scrollbar = tk.Scrollbar(app)
 Def_Scrollbar.pack(side='right',fill='y')
 Themed_Scrollbar = ttk.Scrollbar(app,orient='horizontal')
 Themed_Scrollbar.pack(side='top',fill='x')

 # Entry Widgets
 Def_Entry = tk.Entry(app)
 Def_Entry.pack()
 Themed_Entry = ttk.Entry(app)
 Themed_Entry.pack()

 # Create GUI
 frame_tasks = tkinter.Frame(root)
 frame_tasks.pack()

 listbox_tasks = tkinter.Listbox(frame_tasks, height=10, width=50)
 listbox_tasks.pack(side=tkinter.LEFT)

 scrollbar_tasks = tkinter.Scrollbar(frame_tasks)
 scrollbar_tasks.pack(side=tkinter.RIGHT, fill=tkinter.Y)

 listbox_tasks.config(yscrollcommand=scrollbar_tasks.set)
 scrollbar_tasks.config(command=listbox_tasks.yview)

 entry_task = tkinter.Entry(root, width=50)
 entry_task.pack()

 button_add_task = tkinter.Button(root, text="Add a task", width=48, command=add_task)
 button_add_task.pack()

 button_delete_task = tkinter.Button(root, text="Delete a task", width=48, command=delete_task)
 button_delete_task.pack()

 button_load_tasks = tkinter.Button(root, text="Load a task list", width=48, command=load_tasks)
 button_load_tasks.pack()

 button_save_tasks = tkinter.Button(root, text="Save your task list", width=48, command=save_tasks)
 button_save_tasks.pack()

 root = tkinter()
 root.mainloop()
4 Answers

repl.it has nothing to do with the issue. You have lines referencing "tk", like app = tk.Tk(), but you never defined anything called tk. It looks like some of your code expects you to have imported Tkinter via import tkinter as tk, in which case tk would be valid. But you also have code expecting it to be called tkinter, like root = tkinter.Tk(). It seems like your code was inspired from multiple sources, some of which had Tkinter imported as tk, and some where it was imported as tkinter. All you have to do is replace all tks with tkinter. For example, this line:

Def_Btn = tk.Button(app,text='Default Button')

would become:

Def_Btn = tkinter.Button(app,text='Default Button')

The problem is probably coming from your import

I observed that you did not import Tkinter as tk but you keep on using the tk. try importing it

Since you have:

import tkinter

Python looks for tkinter not tk since you have not told python that you want to use the module with the alias 'tk'

Your solution is:

import tkinter as tk

You probably forgot that you were using tkinter instead of tk.

The problem is from your import statement, you imported tkinter as ttk but you kept on using tk

Related