无法访问属于同一类其他函数的函数中的变量?

时间:2019-11-02 15:57:04

标签: python tkinter

我是OOPS的新手,正在python中用tkinter创建一个GUI。我试图访问属于同一类中其他函数的函数中的变量。由于'labels'对象的错误没有属性'bgcolor '.bgcolor属于lbl_property函数,我正在写函数中访问它。在第85行出现错误 追溯:

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)

1 个答案:

答案 0 :(得分:1)

您的类实例在首次实例化时没有bgcolor属性,因为__init__中没有赋值。

>>> class Labels:
...     def __init__(self,root,prop,title):
            self.root=root
            self.prop=prop
            self.counter=1
            self.title=title   

>>> l = Labels(1,2,3)
>>> hasattr(l,'bgcolor')
False
>>>

在{lbl_property`方法中定义了bgcolor属性

>>> class Labels:
...     def __init__(self,root,prop,title):
            self.root=root
            self.prop=prop
            self.counter=1
            self.title=title
        def lbl_property(self,event):
            self.bgcolor = 5 

您需要先调用该方法

>>> l = Labels(1,2,3)
>>> l.lbl_property('foo')
>>> hasattr(l,'bgcolor')
True
>>> 

或将其添加到__init__

>>> class Labels:
...     def __init__(self,root,prop,title):
            self.root=root
            self.prop=prop
            self.counter=1
            self.title=title
            self.bgcolor=None

>>> l = Labels(1,2,3)
>>> hasattr(l,'bgcolor')
True
>>>