Я новичок в OOPS. Я создаю графический интерфейс с tkinter в python. Я пытаюсь получить доступ к переменной в функции, которая принадлежит другой функции в том же классе. Ошибка при получении, так как объект меток не имеет атрибута bgcolor'.bgcolor принадлежит функции lbl_property, и я обращаюсь к ней в функции записи. Ошибка получения в строке №: 85 Traceback:
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "E:\Colsoftware\Python1\Created Python\Tkinter IDE\Page.py", line 138, in write
self.obj.write(self.dict1)
File "E:\Colsoftware\Python1\Created Python\Tkinter IDE\label.py", line 85, in write
string='\n'+j+'='+'Label('+self.title+','+'bg='+self.bgcolor+','+'fg='+self.fgcolor+','+'width='+self.width+','+'height='+self.height+','+'anchor='+self.loc_var1.get()
AttributeError: 'labels' object has no attribute 'bgcolor
Программа
from tkinter import *
from dnd import *
class labels:
def __init__(self,root,prop,title):
self.root=root
self.prop=prop
self.counter=1
self.title=title
#=========================
def fcounter(self):
self.counter+=1
#-----------------
def label(self,dict1):
self.name_var=Label(self.root,text='Label'+str(self.counter))
self.name_var.pack()
self.name_var.bind("<Button-2>",self.lbl_property)
make_draggable(self.name_var)
if 'label_widget' in dict1.keys():
dict1['label_widget'].append('label'+str(self.counter))
else:
dict1['label_widget']=['label'+str(self.counter)]
self.fcounter()
print(dict1)
def lbl_property(self,event):
#====================
self.lbl_variable=Label(self.prop,text="Variable",font=("",13,""),anchor="w",width=18,bg="white")
self.lbl_variable.place(x=10,y=10)
self.lbl_bgcolor=Label(self.prop,text="Background Color",font=("",13,""),anchor="w",width=18,bg="white")
self.lbl_bgcolor.place(x=10,y=50)
self.lbl_fgcolor=Label(self.prop,text="Text Color",font=("",13,""),width=18,anchor="w",bg="white")
self.lbl_fgcolor.place(x=10,y=90)
self.lbl_width=Label(self.prop,text="Width",font=("",13,""),anchor="w",width=18,bg="white")
self.lbl_width.place(x=10,y=130)
self.lbl_height=Label(self.prop,text="Height",font=("",13,""),anchor="w",width=18,bg="white")
self.lbl_height.place(x=10,y=170)
self.lbl_anchor=Label(self.prop,text="Anchor",font=("",13,""),anchor="w",width=18,bg="white")
self.lbl_anchor.place(x=10,y=210)
#======================
self.loc_var1=StringVar(self.prop)
self.loc_var1.set("center")
self.bgcolor="white"
self.fgcolor="black"
self.width=10
self.height=1
#==========================
self.ety_variable=Entry(self.prop,font=("",13,""), borderwidth=2, relief="groove")
self.ety_variable.place(x=190,y=10)
self.ety_variable.insert(0,self.name_var)
self.ety_bgcolor=Entry(self.prop,font=("",13,""), borderwidth=2, relief="groove")
self.ety_bgcolor.place(x=190,y=50)
self.ety_fgcolor=Entry(self.prop,font=("",13,""), borderwidth=2, relief="groove")
self.ety_fgcolor.place(x=190,y=90)
self.ety_width=Entry(self.prop,font=("",13,""), borderwidth=2, relief="groove")
self.ety_width.place(x=190,y=130)
self.ety_height=Entry(self.prop,font=("",13,""), borderwidth=2, relief="groove")
self.ety_height.place(x=190,y=170)
self.opt_anchor=OptionMenu(self.prop,self.loc_var1,"center","n","s","w","e","ne","nw","sw","se")
self.opt_anchor.config(width=24, relief="groove",bg="white",highlightthickness=0)
self.opt_anchor.place(x=190,y=210)
#===========
self.btn_apply=Button(self.prop,text="Apply",command=lambda:self.apply(event), borderwidth=2, relief="groove",width="20",bg="white")
self.btn_apply.place(x=150,y=650)
#=================
def apply(self,event):
if self.ety_bgcolor.get()!="":
self.bgcolor=self.ety_bgcolor.get()
if self.ety_fgcolor.get()!="":
self.fgcolor=self.ety_fgcolor.get()
if self.ety_width.get()!="":
self.width=self.ety_width.get()
if self.ety_height.get()!="":
self.height=self.ety_height.get()
if self.loc_var1.get()!="center":
self.loc_var1.set(self.loc_var1.get())
event.widget.configure(bg=self.bgcolor,fg=self.fgcolor,width=self.width,height=self.height,anchor=self.loc_var1.get())
def write(self,dict1):
fwrite=open('dummy.py','a')
for i in dict1.values():
for j in i:
self.wname=str(i).replace('.!toplevel.!','')
print(self.wname)
string='\n'+j+'='+'Label('+self.title+','+'bg='+self.bgcolor+','+'fg='+self.fgcolor+','+'width='+self.width+','+'height='+self.height+','+'anchor='+self.loc_var1.get()
fwrite.write(string)