在tkinter中更改标签文本

时间:2019-12-11 02:14:22

标签: python tkinter

我尝试使用tkinter制作基本的笔记程序,将用户输入保存到要显示的文本文件中。我在更改用于显示标签的文本时遇到了麻烦,而Google一直没有帮助。

这是代码

import tkinter as tk
from tkinter import ttk
from time import sleep
class sticky:
    def get_data(self):
        temp = open('sticky_note.txt', 'r')
        r = temp.read()
        self.data = r
        temp.close()

    def open_file(self):
        try:
            self.get_data
            self.notes = open('sticky_note.txt', 'a+')
        except:
            self.notes = open('sticky_note.txt', 'w+')

    def __init__(self):
        self.window = tk.Tk()
        self.window.title('StickyNote')
        self.window.geometry('150x250')
        self.data = ''
        self.get_data()
        self.notes = None
        self.open_file()
        self.entryBox = None
        self.list = None
        self.add = None
        self.create()
    def create(self):
        entry = ttk.Label(self.window, text='Enter a reminder:')
        entry.grid(row=0, column=1)
        self.entryBox = ttk.Entry(self.window)
        self.entryBox.grid(row=1, column=1)
        self.add = ttk.Button(self.window, text='Add reminder')
        self.add.grid(row=2, column=1)
        self.add['command'] = self.remind
        listLabel = ttk.Label(self.window, text='Your reminders:')
        listLabel.grid(row=3, column=1)
        self.list = ttk.Label(self.window, text=self.data)
        self.list.grid(row=4, column=1)
        exit = ttk.Button(self.window, text='Save and exit')
        exit.grid(row=5, column=1)
        exit['command'] = self.quit

    def remind(self):
        self.notes.write(self.entryBox.get() + '\n')
        self.get_data
        self.list['text'] =self.data
    def quit(self):
        self.notes.close()
        self.window.destroy()
def main():
    sleep(1)
    program = sticky()
    program.window.mainloop()

main()

该如何解决?

0 个答案:

没有答案