【问题标题】:How to append text to a label using text from a file and an entry in tkinter python如何使用文件中的文本和 tkinter python 中的条目将文本附加到标签
【发布时间】:2017-11-23 19:28:41
【问题描述】:

我正在制作一个 tkinter python 项目,这是一个待办事项列表。待办事项标签使用“To-Do List.txt”文件中的文本,其内容只是说“待办事项:”

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

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

【问题讨论】:

    标签: python file tkinter python-3.6 tkinter-entry


    【解决方案1】:
    Display['text'] = Display['text'] + ToDoAdd
    

    或:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多