python - Destroying widget in tkinter -


i have managed widget appear calling function, make disappear using destroy method.

unfortunately, way managed making object global, understand bad way of doing things. have tried several times destroy object without using global, none of them worked.

here's relevant code:

def hayhijos():     print("ii")     z=var9.get()     if z==1:         var10 = stringvar()         global numhijosmat         numhijosmat=optionmenu(app,var10,1,2,3,4,5,6,7,8,9,10)         numhijosmat.grid(column=2)     elif z==0:         print("aa")              numhijosmat.destroy() var9 = intvar() hijosentrepartes = checkbutton(app, text="hijos entre las partes", variable=var9, command=hayhijos) hijosentrepartes.var=var9 hijosentrepartes.grid() 

two general possibilities.

  • either create class context keep track of elements such widgets using class variables (self.widget example). therefor have @ class documentation
  • you return , pass widget / function.

    not suitable callbacks general approach

    def function(var, widget=none):     """ widget optional argument. if not passed, initialized none """     if var.get()==1:         # code create widget         # make sure use 'widget = ...' create          # have reference return          # call @ end of function     else:         # code destroy widget         # e.g. widget.destroy()     return widget 

    using code makes easy use widget without making global. variable used in global behaviour in code. "var9". should pass 1 on. need adapt callback using maybe lambda pass both. more recommended class approach on here lambdas lack in scope of readability of code , reusability of code. (could call "bad habit" imho highly influenced use case)

if want reuse widget , want make appear / disappear use of checkbutton suggests, rather recommend grid_remove method instead of widget.destroy method


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -