从另一个类方法

时间:2018-04-14 07:16:51

标签: python function class methods tkinter

所以,我有这个GUI用Button创建一个大的空框架,单击按钮将调用一个函数(方法?),在(不同类)中生成一个较小的框架,带有输入字段,以及在较小的框架下创建一个重复的按钮,一遍又一遍地重复该过程,用较小的框架填充GUI。每个小框架都配有一个“删除”按钮,可以破坏小框架本身...我的问题是我也想要破坏它下面的按钮,这是由不同类中的方法生成的(大类的类)帧)。我似乎无法获得'delete_aircraft'函数来销毁Button ...我已经注释掉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()

1 个答案:

答案 0 :(得分:0)

只要您使用place算法来整理窗口小部件,您就不必关心窗口小部件引用的存储位置。您可以将其存储为self.newaircraft,而不是将Button存储为FleetCreation(即存储为addnewframe.newaircraft实例的属性)(即存储为{{1}实例的属性})。因此,您可以安全地取消注释最后一行,因为New_Aircraft现在是newaircraft的属性。但是,我会颠倒两行:首先销毁self然后销毁self.newaircraft