通过 widget.destroy() 使用内存

问题描述 投票:0回答:1

首先,对我的英语沟通能力感到抱歉,我正在学习。我对 Tkinter 中的 widget.destroy() 有一个小问题。我读到,如果你想销毁一个小部件,destroy =从内存中删除,因为你不会重用,你应该使用这个函数。但是我正在使用它们并且它们没有从内存中删除我在创建新闻小部件并与下一个数字一起出现时看到了它

我的代码仅创建 10 个不同的按钮,正如您在屏幕截图中看到的那样,我有 20 个按钮。这是因为我两次单击按钮。我的问题不是如何删除/隐藏和创建/显示它,我的问题是使用内存并使其优化。我想了解其背后的逻辑。

这是我的代码,它并不完美,但这是一个小测试,有一些截图:

import tkinter as tk
from utils.geometrIrregular import FigurasGeometricas_irregulares

minpady = 10
minpadx = 10

class Root(tk.Tk):
    def __init__( self):
        super().__init__()
        # Menu Buttons
        tk.Button( self, text = "Geometria",
                        command = lambda: self.center_geometry(),
                        width = 10, height = 5, name = 'geometria'
                        ).place( relx = .01, rely = .2)
        tk.Button( self, text = "Calculadora",
                             command = lambda: self.center_calculate(),
                             width = 10, height = 5, name = 'calculadora'
                             ).place( relx = .01, rely = .4)      
        
    def center_geometry( self) -> None:
        self.clean_frame()
        tk.Label( self, text = "Inserta las coordenadas: "
                               ).place( relx = .12, rely = .1)
        
        tk.Entry( self,
                 ).place( relx = .23, rely = .1)
        """ bind options:
            FocusIn, FocusOut, <Button-1>, <KeyPress>... -> some options
        """        
        tk.Button( self, text = "Crear polinomio"
                  ).place( relx = .2, rely = .15)        
             
    def center_calculate( self) -> None:
        """ we are going to create the tk.widgets in the central part
        """
        self.clean_frame()
        self.memory_lst = []
        self.calculate_history = tk.StringVar()
        self.value = tk.StringVar()
        self.result = tk.StringVar()
        self.memory = tk.StringVar()
        
        tk.Label( self,
                 text = 'Last operation:'
                 ).place( relx = .2, rely = .1)
        tk.Entry( self,
                background = 'light grey',
                textvariable = self.calculate_history
                ).place( relx = .25, rely = .1)
        
        tk.Label( self,
                 text = 'Result:'
                ).place( relx = .2, rely = .15)
        tk.Label( self,
                 background = 'snow',
                 textvariable = self.result
                ).place( relx = .25, rely = .15)
        
        tk.Label( self,
                 text = 'Operation:'
                ).place( relx = .20, rely = .2)
        tk.Entry( self,
                 background = 'white',
                 textvariable = self.value
                ).place( relx = .25, rely = .2)
        
        # First buttons row
        tk.Button( self, text = " + "
                  ).place( relx = .25, rely = .25)
        tk.Button( self, text = " - "
                  ).place( relx = .27, rely = .25)
        tk.Button( self, text = " / "
                  ).place( relx = .29, rely = .25)
        tk.Button( self, text = " * "
                  ).place( relx = .31, rely = .25)
        
        # Second buttons row
        tk.Button( self, text = 'M+'
                            ).place( relx = .25, rely = .28)
        tk.Button( self, text = 'M'
                            ).place( relx = .27, rely = .28)
        tk.Button( self, text = 'M-'
                            ).place( relx = .29, rely = .28)
        tk.Button( self, text = ' ^ '
                            ).place( relx = .31, rely = .28)
        
        # Thrid buttons row
        tk.Button( self, text = ' √ '
                            ).place( relx = .25, rely = .31)
        # Memory Lst_Box
        self.memory_lstbox = tk.Listbox( self, background = 'white', listvariable = self.memory,
                              width = 50, height = 50, selectmode = tk.SINGLE
                              ).place( relx = .6, rely = .1)
    # CLEAN BLOCK
    def clean_frame( self):
        for widget in self.winfo_children():
            print( widget.winfo_id)
            if not ('geometria' in str(widget) or 'calculadora' in str(widget)):
                widget.destroy()
                
            
if __name__ == "__main__":
    App = Root()
    App.mainloop()
<bound method Misc.winfo_id of <tkinter.Button object .geometria>>
<bound method Misc.winfo_id of <tkinter.Button object .calculadora>>
<bound method Misc.winfo_id of <tkinter.Button object .geometria>>
<bound method Misc.winfo_id of <tkinter.Button object .calculadora>>
<bound method Misc.winfo_id of <tkinter.Label object .!label>>
<bound method Misc.winfo_id of <tkinter.Entry object .!entry>>
<bound method Misc.winfo_id of <tkinter.Label object .!label2>>
<bound method Misc.winfo_id of <tkinter.Label object .!label3>>
<bound method Misc.winfo_id of <tkinter.Label object .!label4>>
<bound method Misc.winfo_id of <tkinter.Entry object .!entry2>>
<bound method Misc.winfo_id of <tkinter.Button object .!button>>
<bound method Misc.winfo_id of <tkinter.Button object .!button2>>
<bound method Misc.winfo_id of <tkinter.Button object .!button3>>
<bound method Misc.winfo_id of <tkinter.Button object .!button4>>
<bound method Misc.winfo_id of <tkinter.Button object .!button5>>
<bound method Misc.winfo_id of <tkinter.Button object .!button6>>
<bound method Misc.winfo_id of <tkinter.Button object .!button7>>
<bound method Misc.winfo_id of <tkinter.Button object .!button8>>
<bound method Misc.winfo_id of <tkinter.Button object .!button9>>
<bound method Misc.winfo_id of <tkinter.Listbox object .!listbox>>
<bound method Misc.winfo_id of <tkinter.Button object .geometria>>
<bound method Misc.winfo_id of <tkinter.Button object .calculadora>>
<bound method Misc.winfo_id of <tkinter.Label object .!label5>>
<bound method Misc.winfo_id of <tkinter.Entry object .!entry3>>
<bound method Misc.winfo_id of <tkinter.Button object .!button10>>
<bound method Misc.winfo_id of <tkinter.Button object .geometria>>
<bound method Misc.winfo_id of <tkinter.Button object .calculadora>>
<bound method Misc.winfo_id of <tkinter.Label object .!label6>>
<bound method Misc.winfo_id of <tkinter.Entry object .!entry4>>
<bound method Misc.winfo_id of <tkinter.Label object .!label7>>
<bound method Misc.winfo_id of <tkinter.Label object .!label8>>
<bound method Misc.winfo_id of <tkinter.Label object .!label9>>
<bound method Misc.winfo_id of <tkinter.Entry object .!entry5>>
<bound method Misc.winfo_id of <tkinter.Button object .!button11>>
<bound method Misc.winfo_id of <tkinter.Button object .!button12>>
<bound method Misc.winfo_id of <tkinter.Button object .!button13>>
<bound method Misc.winfo_id of <tkinter.Button object .!button14>>
<bound method Misc.winfo_id of <tkinter.Button object .!button15>>
<bound method Misc.winfo_id of <tkinter.Button object .!button16>>
<bound method Misc.winfo_id of <tkinter.Button object .!button17>>
<bound method Misc.winfo_id of <tkinter.Button object .!button18>>
<bound method Misc.winfo_id of <tkinter.Button object .!button19>>
<bound method Misc.winfo_id of <tkinter.Listbox object .!listbox2>>
python tkinter optimization memory-management
1个回答
0
投票

您的代码仅打印

clean_frame
函数中的内容。每次调用该函数时,它都会打印
root
的内容。

您的输出看起来像这样,不是因为

.destroy()
没有删除小部件,而是因为该函数在输出末尾打印(最后一个函数完成打印的位置),从而产生了一个非常长的列表。

我只打印了根目录中小部件的数量(在

clean_frame
函数的开头),因此输出将更具可读性,因此您可以在应用程序中跟踪小部件。

您在输出中收到了这么多小部件,因为您根本没有格式化输出,因此输出来自您所做的所有点击。

修改后的功能:

    def clean_frame( self):
        print(len(self.winfo_children()))
        for widget in self.winfo_children():
            print( widget.winfo_id)
            if not ('geometria' in str(widget) or 'calculadora' in str(widget)):
                widget.destroy()

这就是我的输出:

2
<bound method Misc.winfo_id of <tkinter.Button object .geometria>>
<bound method Misc.winfo_id of <tkinter.Button object .calculadora>>
18
<bound method Misc.winfo_id of <tkinter.Button object .geometria>>
<bound method Misc.winfo_id of <tkinter.Button object .calculadora>>
<bound method Misc.winfo_id of <tkinter.Label object .!label>>
<bound method Misc.winfo_id of <tkinter.Entry object .!entry>>
<bound method Misc.winfo_id of <tkinter.Label object .!label2>>
<bound method Misc.winfo_id of <tkinter.Label object .!label3>>
<bound method Misc.winfo_id of <tkinter.Label object .!label4>>
<bound method Misc.winfo_id of <tkinter.Entry object .!entry2>>
<bound method Misc.winfo_id of <tkinter.Button object .!button>>
<bound method Misc.winfo_id of <tkinter.Button object .!button2>>
<bound method Misc.winfo_id of <tkinter.Button object .!button3>>
<bound method Misc.winfo_id of <tkinter.Button object .!button4>>
<bound method Misc.winfo_id of <tkinter.Button object .!button5>>
<bound method Misc.winfo_id of <tkinter.Button object .!button6>>
<bound method Misc.winfo_id of <tkinter.Button object .!button7>>
<bound method Misc.winfo_id of <tkinter.Button object .!button8>>
<bound method Misc.winfo_id of <tkinter.Button object .!button9>>
<bound method Misc.winfo_id of <tkinter.Listbox object .!listbox>>
5
<bound method Misc.winfo_id of <tkinter.Button object .geometria>>  
<bound method Misc.winfo_id of <tkinter.Button object .calculadora>>
<bound method Misc.winfo_id of <tkinter.Label object .!label5>>     
<bound method Misc.winfo_id of <tkinter.Entry object .!entry3>>     
<bound method Misc.winfo_id of <tkinter.Button object .!button10>>  
18
© www.soinside.com 2019 - 2024. All rights reserved.