-1

I'm writing a simple to-do list program with Tkinter and I've ran in a problem: the "Add task" button doesn't do anything! Please tell me what's wrong with my program and how to fix it. Here's the code:

import tkinter as tk

tasks = ["Enter a new task...", "Enter a new task...", "Enter a new task..."]

window = tk.Tk()
title = tk.Label(text="To-Do List")
t0 = tk.Label(text=tasks[0])
t1 = tk.Label(text=tasks[1])
t2 = tk.Label(text=tasks[2])

def addTask():
    count = 0
    tasks[count] = newTask.get()
    count += 1
    if count == 3:
        count = 0

newTask = tk.Entry()
newTaskButton = tk.Button(text="Add task", command=addTask())

t0.pack()
t1.pack()
t2.pack()
newTask.pack()
newTaskButton.pack()
window.mainloop()
  • To answer your titular question: To change the text of an existing `Label`, call its [`config()`](https://docs.python.org/3/library/tkinter.html#setting-options) method: i.e. `t1.config(text='new content')`. – martineau Dec 10 '21 at 22:05

1 Answers1

0
command=addTask()

should be

command=addTask

This is because the button receives the address or id of the function to call and does the call inside the button process. addTask() calls the addTask function directly and will not work.

martineau
  • 119,623
  • 25
  • 170
  • 301
InhirCode
  • 338
  • 1
  • 4
  • 5