Ошибка `'NoneType' object has no attribute '_root'` при создании `StringVar`
Вот код:
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"
CV = StringVar(value=Black)
FIN = []
GNFN = ""
FTV = ""
F = ""
LY = 30
OP = ""
root = Tk()
root.title("FILE READER")
root.geometry("2500x1000")
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)
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)
root.mainloop()
Строчка с выдаваемой ошибкой находится в самом начале.
Вот ошибка:
Traceback (most recent call last):
File "C:\Users\Sergei\Desktop\Text-file-creator\Text file creator.py", line 14, in <module>
CV = StringVar(value=Black)
File "C:\Users\Sergei\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line
505, in __init__
Variable.__init__(self, master, value, name)
File "C:\Users\Sergei\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line
335, in __init__
self._root = master._root()
AttributeError: 'NoneType' object has no attribute '_root'
Я не понимаю что это за ошибка, помогите пожалуйста.
Источник: Stack Overflow на русском