codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to correctly use 'in_' argument in tkinter grid()?


Peter Otten? 2019?9?10???? UTC+8??5?04?27????
> jfong at ms4.hinet.net wrote:
> 
> > I had tried the following script test.py:
> > --------
> > import tkinter as tk
> > 
> > class Demo(tk.Frame):
> >     def __init__(self):
> >         tk.Frame.__init__(self, name='demo')
> >         self.pack()
> >         
> >         panel = tk.Frame(self, name='panel')
> >         panel.pack()
> >         
> >         start = tk.Button(text='Start', name='start')
> >         start.grid(in_=panel)
> >         
> >         btn = self.nametowidget('panel.start')
> >         btn.config(state='disabled')
> > 
> > Demo().mainloop()
> > --------
> > 
> > It fails on nametowidget() function. My intention is to use 'in_' to
> > change the parent of 'start' widget from the default Tk object to 'panel',
> > but failed with KeyError: 'start'.
> > 
> > below is part of the snapshot in pdb,
> > ...
> >> d:\works\python\test.py(11)__init__()
> > -> start = tk.Button(text='Start', name='start')
> > (Pdb) !panel.winfo_parent()
> > '.demo'
> > (Pdb) next
> >> d:\works\python\test.py(12)__init__()
> > -> start.grid(in_=panel)
> > (Pdb) !start.winfo_parent()
> > '.'
> > (Pdb) next
> >> d:\works\python\test.py(14)__init__()
> > -> btn = self.nametowidget('panel.start')
> > (Pdb) !start.winfo_parent()
> > '.'
> > 
> > --Jach
> 
> I think that the `in_` argument is used correctly. It's just that your 
> expectation that the name is changed to reflect the layout hierarchy is 
> wrong. 
> 
> To manipulate the start button you can use the `start` variable directly:
> 
> start.config(state='disabled')
> 
> To find all slaves of the panel use
> 
> panel.grid_slaves()
> 
> $ cat grid_in.py
> import tkinter as tk
> 
> class Demo(tk.Frame):
>     def __init__(self):
>         tk.Frame.__init__(self, name='demo')
>         self.pack()
>         
>         self.panel = panel = tk.Frame(self, name='panel')
>         panel.pack()
>         
>         start = tk.Button(text='Start', name='start')
>         start.grid(in_=panel)
>         
>         for btn in panel.grid_slaves():
>             print("disabling", btn._w)
>             btn.config(state='disabled')
> 
> Demo() #.mainloop()
> $ python3 grid_in.py 
> disabling .start
> $

Oh, I was misunderstanding the purpose of 'in_'. No wonder it's showing up in the .grid() manager:-) Thank you.

--Jach