0

При нажатии на кнопку save и delete появляется ошибка "NameError: name 'todolist' is not defined"

from tkinter import * 
from tkinter import filedialog

win =Tk()
win.geometry('300x300')
win.title('Registries')
win.resizable(False,False)


def clear():
  todolist.delete(0.0,END)


def Save():
  global todolist
  text = todolist.get(0.0,END)
  file = filedialog.asksaveasfile(title='Save name',filetypes=(('txt files','*.txt')('allfiles','*.*')))
  if file:
    file.write(text)


def Create_a_reestr():

  create_win = Tk()
  create_win.geometry('500x500')
  create_win.title('Create a registry')

  todolist = Text(create_win,width=50,height=20)
  todolist.place(x=50,y=20)

  save = Button(create_win,text='Save',bg='white',fg='black',width=25,height=2,command=Save)
  save.place(x=165,y=380)

  delete = Button(create_win,text='Delet text',bg='white',fg='black',width=25,height=2,command=clear)
  delete.place(x=165,y=430)

  create_win.mainloop()


create_reestr = Button(text='Create reestr',bg='white',fg = 'black',width=20,command=Create_a_reestr)
create_reestr.place(x=85,y=100)


win.mainloop()
1
  • Так у вас todolist - это локальная переменная в функции Create_a_reestr. С чего вы решили, что остальные функции должны её видеть? Объявляйте её global там где присваиваете ей значение.
    – GrAnd
    30 апр 2021 в 21:07

1 ответ 1

0

Объясню, у вас todolist локальная переменная функции Create_a_reestr. Чтобы она стало глобальной, ее необходимо объявить до вызова функций. Вообще, лучше не заводить глобальных переменных, но ладно) В общем вот ваш код, исправил еще ошибку с типами файлов:

from tkinter import *
from tkinter import filedialog

win = Tk()
win.geometry('300x300')
win.title('Registries')
win.resizable(False, False)


def clear():
    global todolist
    todolist.delete(0.0, END)


def Save():
    global todolist
    text = todolist.get(0.0, END)
    file = filedialog.asksaveasfile(title='Save name', filetypes=[('txt files', '*.txt'), ('allfiles', '*.*')])
    if file:
        file.write(text)


def Create_a_reestr():
    global todolist
    create_win = Tk()
    create_win.geometry('500x500')
    create_win.title('Create a registry')

    todolist = Text(create_win, width=50, height=20)
    todolist.place(x=50, y=20)

    save = Button(create_win, text='Save', bg='white', fg='black', width=25, height=2, command=Save)
    save.place(x=165, y=380)

    delete = Button(create_win, text='Delet text', bg='white', fg='black', width=25, height=2, command=clear)
    delete.place(x=165, y=430)

    create_win.mainloop()


todolist = None
create_reestr = Button(text='Create reestr', bg='white', fg='black', width=20, command=Create_a_reestr)
create_reestr.place(x=85, y=100)

win.mainloop()

И еще, Пожалуйста, никогда не называйте функции с большой буквы! обязательно почитайте о pep8 (руководство по написанию кода на Python)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.