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

≡放荡痞女 提交于 2019-12-13 03:15:19

问题


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()

回答1:


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

or:

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


来源:https://stackoverflow.com/questions/47462282/how-to-append-text-to-a-label-using-text-from-a-file-and-an-entry-in-tkinter-pyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!