Python 从另一个类方法引用类对象

Python 从另一个类方法引用类对象,python,function,class,methods,tkinter,Python,Function,Class,Methods,Tkinter,因此,我有一个GUI,它用一个按钮创建一个大的空框架,单击按钮将调用一个函数(方法?),该函数在(不同类的)中生成一个较小的框架,带有输入字段,并在较小的框架下创建一个重复的按钮,反复重复这个过程,用较小的框架填充GUI。每个小框架都配备了一个“删除”按钮,该按钮会销毁小框架本身……我的问题是,我还想让它销毁它下面的按钮,该按钮是由不同类(大框架的类)中的方法生成的。我似乎无法使用“删除飞机”功能来破坏按钮。。。我已经注释掉了delete_aircraft函数的那一行,因为我知道在这种情况下引用

因此,我有一个GUI,它用一个按钮创建一个大的空框架,单击按钮将调用一个函数(方法?),该函数在(不同类的)中生成一个较小的框架,带有输入字段,并在较小的框架下创建一个重复的按钮,反复重复这个过程,用较小的框架填充GUI。每个小框架都配备了一个“删除”按钮,该按钮会销毁小框架本身……我的问题是,我还想让它销毁它下面的按钮,该按钮是由不同类(大框架的类)中的方法生成的。我似乎无法使用“删除飞机”功能来破坏按钮。。。我已经注释掉了delete_aircraft函数的那一行,因为我知道在这种情况下引用“self”是不正确的。当“newaircraft”按钮由不同类中的方法创建时,如何摆脱它的每个实例

基本上我想反转'addnew'函数的操作

代码如下:

class Fleet_Creation(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, width=1000, height=700, bg='grey80', relief='ridge', bd=3)
        fleet_title=tk.Label(text='Fleet Creation Module', font='arial 20 bold', bg='grey80', justify='center')
        fleet_title.place(anchor='nw', x=350, y=5)
        self.newaircraft = tk.Button(text='Add New Aircraft...', font='arial 15', relief='raised', bd=3, bg='SteelBlue1', command=self.addnew)
        self.newaircraft.place(anchor='nw', x=78, y=45)

    def addnew(self):
        xcoord = self.newaircraft.winfo_x()
        ycoord = self.newaircraft.winfo_y()
        self.newaircraft.destroy()
        addnewframe = New_Aircraft(Fleet_Creation)
        if ycoord<600:
            addnewframe.place(anchor='nw', x=(xcoord-80), y=ycoord)
            self.newaircraft = tk.Button(text='Add New Aircraft...', font='arial 15', relief='raised', bd=3, bg='SteelBlue1', command=self.addnew)
            self.newaircraft.place(anchor='nw', x=(xcoord), y=(ycoord+100))
        elif ycoord>=600:
            addnewframe.place(anchor='nw', x=(xcoord+253), y=(ycoord-ycoord+47))
            self.newaircraft = tk.Button(text='Add New Aircraft...', font='arial 15', relief='raised', bd=3, bg='SteelBlue1', command=self.addnew)
            self.newaircraft.place(anchor='nw', x=(xcoord+333), y=(ycoord-ycoord+148))
        elif xcoord > 800:
            self.newaircraft.destroy()


class New_Aircraft(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, width=333, height=100, bg='grey60', relief='ridge', bd=5 )
        n_number=tk.Label(self, text='N-Number:', font='arial 10 bold', bg='grey60')
        n_number.place(anchor='nw', x=3, y=3)
        n_number_entry=tk.Entry(self, font='arial 10 bold', relief='sunken', bd=2, width=7)
        n_number_entry.place(anchor='nw', x=75, y=3)
        type=tk.Label(self, text='Aircraft Type(ICAO):', font='arial 10 bold', bg='grey60')
        type.place(anchor='nw', x=135, y=3)
        type_entry=tk.Entry(self, font='arial 10 bold', width=7, relief='sunken', bd=2)
        type_entry.place(anchor='nw', x=265, y=3)
        aircraft_category = ['CATEGORY...','AIRPLANE','ROTORCRAFT']
        aircraft_class = ['CLASS...','ASEL', 'ASES', 'AMEL', 'AMES']
        aircraft_type = ['TYPE...','JET', 'TURBINE', 'RECIPROCATING', 'GLIDER']
        accat = tk.StringVar()
        accls = tk.StringVar()
        actype = tk.StringVar()
        Category = tk.OptionMenu(self, accat, 'AIRPLANE', 'ROTORCRAFT')
        Class = tk.OptionMenu(self, accls, 'ASEL', 'ASES', 'AMEL', 'AMES')
        Type = tk.OptionMenu(self, actype, 'JET', 'TURBINE', 'RECIPROCATING', 'GLIDER')
        Category.place(anchor='nw', x=2, y=40)
        Class.place(anchor='nw', x=117, y=40)
        Type.place(anchor='nw', x=205, y=40)
        Category.config(bg='grey60', font='arial 8 bold', bd=0)
        Class.config(bg='grey60', font='arial 8 bold', bd=0)
        Type.config(bg='grey60', font='arial 8 bold', bd=0)
        accat.set('CATEGORY...')
        accls.set('CLASS...')
        actype.set('TYPE...')
        h1 = tk.Label(self, font='arial 1', bg='grey60', relief='sunken', width=315)
        h1.place(anchor='nw', x=0, y=28)
        self.addbutton=tk.Button(self, text='ADD', height=2, font='arial 10', bg='DodgerBlue2', command=self.add)
        self.addbutton.place(anchor='nw', x=283, y=40)
        self.delete = tk.Button(self, text='DELETE', font='arial 7 bold', bg='red', width=30, justify='center', command=self.delete_aircraft)
        self.delete.place(anchor='nw',x=45,y=69)


    #create function to render addbutton disabled
    def add(self):
        self.addbutton.config(state='disabled')
        #other stuff

    def delete_aircraft(self):
        self.destroy()
        #destroy 'newaircraft' button created by the addnew function
        #self.newaircraft.destroy()
class Fleet\u创建(tk.Frame):
定义初始化(self,*args,**kwargs):
tk.Frame.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
fleet_title=tk.Label(text='fleet Creation Module',font='arial 20 bold',bg='grey80',justify='center')
船队名称位置(锚定=nw',x=350,y=5)
self.newAircrafter=tk.按钮(text='Add New Aircrafter…',font='arial 15',relief='rised',bd=3,bg='SteelBlue1',command=self.addnew)
自.新飞机.地点(锚点='nw',x=78,y=45)
def ADDNOW(自我):
xcoord=self.newaircraft.winfo_x()
ycoord=self.newaircraft.winfo_y()
self.newAircrafter.destroy()
addnewframe=新飞机(机队创建)
如果ycoord=600:
addnewframe.place(anchor='nw',x=(xcoord+253),y=(ycoord-ycoord+47))
self.newAircrafter=tk.按钮(text='Add New Aircrafter…',font='arial 15',relief='rised',bd=3,bg='SteelBlue1',command=self.addnew)
self.newAircrafter.place(anchor='nw',x=(xcoord+333),y=(ycoord-ycoord+148))
elif xcoord>800:
self.newAircrafter.destroy()
新型飞机(传统框架):
定义初始(自我,主):
tk.Frame.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
n_number=tk.标签(self,text='n-number:',font='arial 10 bold',bg='grey60')
n_编号位置(锚=nw',x=3,y=3)
n\u number\u entry=tk.entry(self,font='arial 10 bold',relieve='sunken',bd=2,width=7)
n\u编号\u入口位置(锚定=nw',x=75,y=3)
type=tk.标签(self,text='Aircraft type(ICAO):',font='arial 10 bold',bg='grey60')
类型.位置(锚=nw',x=135,y=3)
键入项目=tk.entry(self,font='arial 10 bold',width=7,relieve='sunken',bd=2)
输入位置(锚定=nw',x=265,y=3)
飞机类别=[“类别…”、“飞机”、“旋翼机”]
飞机等级=['class…','ASEL','ASES','AMEL','AMES']
飞机类型=[“类型…”、“喷气式”、“涡轮式”、“往复式”、“滑翔式”]
accat=tk.StringVar()
accls=tk.StringVar()
actype=tk.StringVar()
类别=tk.选项菜单(self、accat、‘飞机’、‘旋翼机’)
Class=tk.OptionMenu(self、accls、ASEL、ASES、AMEL、AMES)
类型=tk.选项菜单(self、actype、“JET”、“TURBINE”、“往复”、“GLIDER”)
类别.地点(锚定=nw',x=2,y=40)
类位置(锚定=nw',x=117,y=40)
类型.位置(锚=nw',x=205,y=40)
Category.config(bg='grey60',font='arial 8 bold',bd=0)
Class.config(bg='grey60',font='arial 8 bold',bd=0)
Type.config(bg='grey60',font='arial 8 bold',bd=0)
accat.set('CATEGORY…')
accls.set('CLASS…'))
actype.set('TYPE…')
h1=tk.标签(自,font='arial 1',bg='grey60',浮雕='sunken',宽度=315)
h1.位置(锚定=nw',x=0,y=28)
self.addbutton=tk.Button(self,text='ADD',height=2,font='arial 10',bg='DodgerBlue2',command=self.ADD)
self.addbutton.place(anchor='nw',x=283,y=40)
self.delete=tk.按钮(self,text='delete',font='arial 7 bold',bg='red',width=30,justify='center',command=self.delete_)
self.delete.place(anchor='nw',x=45,y=69)
#创建用于渲染addbutton的函数已禁用
def添加(自我):
self.addbutton.config(state='disabled')
#其他东西
def delete_飞机(自身):
自我毁灭
#销毁addnew功能创建的“newaircraft”按钮
#self.newAircrafter.destroy()

只要您使用
place
算法来组织小部件,您就不会真正关心小部件引用存储在哪里。您可以将按钮存储为
addnewframe.NewAircrafter
(即存储为
FleetCreation
实例的属性),而不是将其存储为
addnewframe.newaircraft
(即存储为
New\u Aircraft
实例的属性)。因此,您可以安全地取消对最后一行的注释,因为
newaircraft
现在是
self
的一个属性。然而,我将颠倒两行:首先销毁
self.newAircrafter
,然后销毁
self