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

jfong at ms4.hinet.net jfong at ms4.hinet.net
Mon Sep 9 23:49:52 EDT 2019


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



More information about the Python-list mailing list