如何使用文件中的文本和tkinter python中的条目将文本追加到标签 [英] How to append text to a label using text from a file and an entry in tkinter python

查看:109
本文介绍了如何使用文件中的文本和tkinter python中的条目将文本追加到标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个tkinter python项目,这是一个待办事项列表.待办事项列表标签使用待办事项列表.txt"文件中的文本,其内容仅显示待办事项列表:"

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:'

我正在尝试将文本输入到输入功能中,当我按下添加项目"按钮时,输入中的文本应添加到标签中的文本上,而标签中的文本基本上是txt文件.该条目应添加到txt文件中.我应该怎么做呢?我不知道怎么.到目前为止,这是我的代码.

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

推荐答案

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

或:

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

这篇关于如何使用文件中的文本和tkinter python中的条目将文本追加到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆