Ошибка "not defined", хотя это не так

Рейтинг: 0Ответов: 1Опубликовано: 18.04.2023

Код таков:

from tkinter import *
from tkinter import font
from tkinter import ttk
import os

def TFN():
    GNFN = CN.get()
    print(GNFN)

Black = "Black"
Blue = "Blue"
Red = "Red"

FIN = []
GNFN = ""
FTV = ""
F = ""
LY = 30
OP = ""


root = Tk()
root.title("FILE READER")
root.geometry("2500x1000")

CV = StringVar(value=Black)

canvas = Canvas(bg="white", width=2500, height=1000)
canvas.pack(anchor=CENTER, expand=1)



def READ_FILE():
    
    
    def OF():
        canvas.delete(LFRID)
        canvas.delete(FRID)
        canvas.delete(OPOID)
        CFR = ttk.Label(text='Please choose the colour of the text')
        CFRID = canvas.create_window(999, 100, window=CFR, width=600, height=200)
        BCC = ttk.Radiobutton(text="Black", variable=CV, value=Black)
        BCCID = canvas.create_window(500, 300, anchor=NW, window=BCC, width=600, height=20)
        BLCC = ttk.Radiobutton(text="Blue", variable=CV, value=Blue)
        BLCCID = canvas.create_window(500, 350, anchor=NW, window=BLCC, width=600, height=20)
        RCC = ttk.Radiobutton(text="Red", variable=CV, value=Red)
        RCCID = canvas.create_window(500, 400, anchor=NW, window=RCC, width=600, height=20)
        CCO = ttk.Button(text="OK", command=CB)
        CCOID = canvas.create_window(999, 530, anchor=NW, window=CCO, width=50, height=50)


    
    FR = ttk.Entry(text="")
    FRID = canvas.create_window(500, 300, anchor=NW, window=FR, width=600, height=200)
    LFR = ttk.Label(text='Please enter the name of the file')
    LFRID = canvas.create_window(999, 100, window=LFR, width=600, height=200)
    OPO = ttk.Button(text="OK", command=OF)
    OPOID = canvas.create_window(999, 530, anchor=NW, window=OPO, width=50, height=50)

def START():
    global LY
    canvas.delete(SBID)
    directory = 'D:/Python files'
    for filename in os.listdir(directory):
        f =  filename
        FIN.append(f)
        canvas.create_text(600, LY, font="Arial 50", anchor=NW, text=f, fill="#004D40")
        LY = LY + 80
    AB = ttk.Button(text="ADD FILE", command=ADD_FILE)
    ABID = canvas.create_window(100, 100, anchor=NW, window=AB, width=300, height=800)
    RB = ttk.Button(text="READ FILE", command=READ_FILE)
    RBID = canvas.create_window(1600, 100, anchor=NW, window=RB, width=300, height=800)

SB = ttk.Button(text="START", command=START)
SBID = canvas.create_window(690, 30, anchor=NW, window=SB, width=600, height=200)




def ADD_FILE():
    def TFN():
        GNFN = NFN.get()
        canvas.delete(NFNID)
        canvas.delete(CNID)
        canvas.delete(LFNID)
        file = open("D:/Python files/" + GNFN + ".txt", "w")
        for filename in os.listdir("D:/Python files"):
            print(filename)
    LFN = ttk.Label(text='Please enter the name of the file')
    LFNID = canvas.create_window(999, 100, window=LFN, width=600, height=200)
    NFN = ttk.Entry(text="")
    NFNID = canvas.create_window(500, 300, anchor=NW, window=NFN, width=600, height=200)
    CN = ttk.Button(text="OK", command=TFN)
    CNID = canvas.create_window(999, 530, anchor=NW, window=CN, width=50, height=50)

def CB():
    canvas.delete(CFRID)
    canvas.delete(BCCID)
    canvas.delete(BLSSID)
    canvas.delete(RCCID)
    canvas.delete(CCOID)

root.mainloop()

В самой последней функции (CB) ни один (по крайней мере первый) пункт удаления неизвестен.

Вот ошибка:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Sergei\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 
1883, in __call__
    return self.func(*args)
  File "C:\Users\Sergei\Desktop\Text-file-creator\Text file creator.py", line 97, in CB
    canvas.delete(CFRID)
NameError: name 'CFRID' is not defined

Помогите пожалуйста.

Ответы

▲ 1Принят

Дело в том, что вы объявляете переменные, используемые этой функции, внутри другой функции. Чтобы решить проблему, вы можете объявить переменные глобально или передавать переменные как аргументы. Ниже приведен код, в котором переменные объявлены глобально:

from tkinter import *
from tkinter import font
from tkinter import ttk
import os

def TFN():
    GNFN = CN.get()
    print(GNFN)

Black = "Black"
Blue = "Blue"
Red = "Red"

FIN = []
GNFN = ""
FTV = ""
F = ""
LY = 30
OP = ""


root = Tk()
root.title("FILE READER")
root.geometry("2500x1000")

CV = StringVar(value=Black)

canvas = Canvas(bg="white", width=2500, height=1000)
canvas.pack(anchor=CENTER, expand=1)



def READ_FILE():
    def OF():
        global CFRID, BCCID, RCCID, CCOID

        canvas.delete(LFRID)
        canvas.delete(FRID)
        canvas.delete(OPOID)
        CFR = ttk.Label(text='Please choose the colour of the text')
        CFRID = canvas.create_window(999, 100, window=CFR, width=600, height=200)
        BCC = ttk.Radiobutton(text="Black", variable=CV, value=Black)
        BCCID = canvas.create_window(500, 300, anchor=NW, window=BCC, width=600, height=20)
        BLCC = ttk.Radiobutton(text="Blue", variable=CV, value=Blue)
        BLCCID = canvas.create_window(500, 350, anchor=NW, window=BLCC, width=600, height=20)
        RCC = ttk.Radiobutton(text="Red", variable=CV, value=Red)
        RCCID = canvas.create_window(500, 400, anchor=NW, window=RCC, width=600, height=20)
        CCO = ttk.Button(text="OK", command=CB)
        CCOID = canvas.create_window(999, 530, anchor=NW, window=CCO, width=50, height=50)


    
    FR = ttk.Entry(text="")
    FRID = canvas.create_window(500, 300, anchor=NW, window=FR, width=600, height=200)
    LFR = ttk.Label(text='Please enter the name of the file')
    LFRID = canvas.create_window(999, 100, window=LFR, width=600, height=200)
    OPO = ttk.Button(text="OK", command=OF)
    OPOID = canvas.create_window(999, 530, anchor=NW, window=OPO, width=50, height=50)

def START():
    global LY
    canvas.delete(SBID)
    directory = 'D:/Python files'
    for filename in os.listdir(directory):
        f =  filename
        FIN.append(f)
        canvas.create_text(600, LY, font="Arial 50", anchor=NW, text=f, fill="#004D40")
        LY = LY + 80
    AB = ttk.Button(text="ADD FILE", command=ADD_FILE)
    ABID = canvas.create_window(100, 100, anchor=NW, window=AB, width=300, height=800)
    RB = ttk.Button(text="READ FILE", command=READ_FILE)
    RBID = canvas.create_window(1600, 100, anchor=NW, window=RB, width=300, height=800)

SB = ttk.Button(text="START", command=START)
SBID = canvas.create_window(690, 30, anchor=NW, window=SB, width=600, height=200)




def ADD_FILE():
    def TFN():
        GNFN = NFN.get()
        canvas.delete(NFNID)
        canvas.delete(CNID)
        canvas.delete(LFNID)
        file = open("D:/Python files/" + GNFN + ".txt", "w")
        for filename in os.listdir("D:/Python files"):
            print(filename)
    LFN = ttk.Label(text='Please enter the name of the file')
    LFNID = canvas.create_window(999, 100, window=LFN, width=600, height=200)
    NFN = ttk.Entry(text="")
    NFNID = canvas.create_window(500, 300, anchor=NW, window=NFN, width=600, height=200)
    CN = ttk.Button(text="OK", command=TFN)
    CNID = canvas.create_window(999, 530, anchor=NW, window=CN, width=50, height=50)

def CB():
    global CFRID, BCCID, BLSSID, RCCID, CCOID

    canvas.delete(CFRID)
    canvas.delete(BCCID)
    canvas.delete(BLSSID)
    canvas.delete(RCCID)
    canvas.delete(CCOID)

root.mainloop()

Также, в функции CB вы используете переменную BLSSID, нигде ранее не объявленную. Я подозреваю, что вы допустили опечатку либо здесь, либо в функции OF