Search code examples
pythonfiletkinterpython-3.6tkinter-entry

How to append text to a label using text from a file and an entry in tkinter python


I am making a tkinter python project, which is a to-do list. The to-do list label uses text from the 'To-Do List.txt' file, whose contents just say 'To Do List:'

I am trying to enter text into an entry function and, when I press the 'Add Item' button, the text in the entry should add to the text in the label, and the text in the label is basically the txt file. The entry should add to the txt file. How should I go about doing this? I do not know how. Here is my code so far.

import tkinter as tk

window = tk.Tk()

List = open('--------------------------/To-Do List.txt','r+')

data = List.read()

Display = tk.Label(window, text = data, anchor = 'w')
ItemName = tk.Entry(window)

def Add():
    global ItemName
    global Display
    global List
    global data
    ToDoAdd = ItemName.get()
    List.write('''
''' + ToDoAdd)
    #what to add here??

Addtem = tk.Button(window, text = 'Add Item', command = Add)

Display.grid(row = 0, column = 0)
ItemName.grid(row = 2, column = 0)
Addtem.grid(row = 3, column = 0)

window.mainloop()

Solution

  • Display['text'] = Display['text'] + ToDoAdd
    

    or:

    Display.config(text=Display.cget('text') + ToDoAdd)