Access state of object (tkinter, Python3) beginner's level
Question: What I want to get: change of checkbox state changes the state of the Entry
widget from 'disabled' into 'normal'. (checkbox off = Entry disabled, checkbox
on = Entry normal). My problem is that I don't know how to access and update
the state of entry.

My code:

    
    
    from tkinter import *
    from tkinter import ttk
    
    class App(Frame):
        def __init__(self, master):
            ttk.Frame.__init__(self, master, padding='20')
            self.grid()
            self.create_checkbox()
            self.create_entry()
    
        def create_checkbox(self):
            self.limit = BooleanVar()
            Checkbutton(self,
                        text='Limit length',
                        variable= self.limit,
                        command= self.state_update,
                        ).grid(row=1, column=1, sticky=W)
    
        def create_entry(self):
            self.entry_low = StringVar()
            Entry(self,
                  width=6,
                  textvariable=self.entry_low,
                  state='disabled',
                  ).grid(row=1, column=2, sticky=W)
    
    
        def state_update(self):
            self.entry_low.config(state="normal")  #THIS OBVIOUSLY DOES NOT WORK
    
    root = Tk()
    root.title("Lottery")
    app = App(root)
    root.mainloop()
    

I'm beginner, so I'd be especially grateful for simple solutions.


Answer: Save a reference to the entry widget, then call the `configure` method. To
make things easy, give your checkbutton the values for the states. That isn't
strictly necessary, you can use a boolean and then translate that to the
appropriate state.

    
    
    def create_checkbox(self):
        self.limit = StringVar(value="normal")
        checkbutton = Checkbutton(..., onvalue="normal", offvalue="disabled", ...)
        checkbutton.grid(...)
    
    def create_entry(self):
        self.entry_low = StringVar()
        self.entry = Entry(self,
              width=6,
              textvariable=self.entry_low,
              state='disabled',
              )
       self.entry.grid(row=1, column=2, sticky=W)
    
    def state_update(self):
        self.entry.config(state="normal")  #THIS OBVIOUSLY DOES NOT WORK
    

* * *

Note: you need to call `grid` in a second step. `grid(...)` (as well as
`place`) returns `None`. If you do `x=Entry(...).grid(...)`, `x` will always
be `None`.

