NameError:name'tk'未定义repl.it
嘿这里的初学者程序员,为高中作业制作待办事项清单,但无法弄清楚这个错误。这也是我第一次使用 python,因为我通常在项目中使用 java 和 JavaScript。我正在尝试为我的程序添加明暗模式。我这样做正确吗?顺便说一句,我正在使用在线 ide repl.it
这是我的代码供参考
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()
回答
repl.it 与问题无关。您有引用“ tk
”的行,例如app = tk.Tk()
,但您从未定义任何名为tk
. 看起来您的某些代码希望您通过 导入 Tkinter import tkinter as tk
,在这种情况下tk
将是有效的。但是你也有代码期望它被调用tkinter
,比如root = tkinter.Tk()
. 似乎您的代码受到多个来源的启发,其中一些将 Tkintertk
导入为tkinter
. 您所要做的就是将所有tk
s替换为tkinter
。例如,这一行:
Def_Btn = tk.Button(app,text='Default Button')
会成为:
Def_Btn = tkinter.Button(app,text='Default Button')