I am currently trying to learn doing simple GUIs with Python using the package Tkinter.
I wrote this program for a To Do List:
import tkinter
import tkinter.messagebox
import pickle
root = tkinter.Tk()
root.title = ("To Do List")
def add_task():
task = entry_task.get()
if task not in ["", "Do nothing"]:
listbox_tasks.insert(tkinter.END, task)
entry_task.delete(0, tkinter.END)
else:
tkinter.messagebox.showwarning(title="Warning!", message="Doing nothing is not an option!.")
def delete_task():
try:
task_index = listbox_tasks.curselection()[0]
listbox_tasks.delete(task_index)
except:
tkinter.messagebox.showwarning(title="Warning!", message="You need to select a task to delete.")
def load_tasks():
try:
tasks = pickle.load(open("Aufgaben.txt", "rb")) # reading binary
listbox_tasks.delete(0, tkinter.END)
for task in tasks:
listbox_tasks.insert(tkinter.END, task)
except: tkinter.messagebox.showwarning(title="Warning!", message="There is no saved file.")
def save_tasks():
tasks = listbox_tasks.get(0, listbox_tasks.size())
pickle.dump(tasks, open("Aufgaben.txt", "wb")) # wb = write binary
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=53)
entry_task.pack()
button_add_task = tkinter.Button(root, text="Add Task", width=48, command=add_task)
button_add_task.pack()
button_delete_task = tkinter.Button(root, text="Delete Task", width=48, command=delete_task)
button_delete_task.pack()
button_load_tasks = tkinter.Button(root, text="Load Tasks", width=48, command=load_tasks)
button_load_tasks.pack()
button_save_tasks = tkinter.Button(root, text="Save Tasks", width=48, command=save_tasks)
button_save_tasks.pack()
root.mainloop()
Everything works like I want it to work, except for the title.
Instead of the correct title, it just displays "tk" in the window bar.
Why does this problem occur?
CodePudding user response:
Try changing the title row to this.
root.title("To Do List")
The = sign is used to assign a value to something. In this case, you are calling the title function/method where you have to input the string of the title you intend to have as a parameter.
CodePudding user response:
root.title()
is a method that requires a string as argument. It is not a attribute you access there.
Example:
root.title("To Do List")
instead of:
root.title = ("To Do List")