0

Using ttk.Checkbutton.instate('selected') to get current state of a checkbutton gives me this error:

File "C:\Program Files\Python\lib\tkinter\ttk.py", line 576, in instate
 self.tk.call(self._w, "instate", ' '.join(statespec)))_tkinter.TclError: Invalid state name s

This is my code:

import tkinter as tk
from tkinter import ttk
def p(event):
    st = ck.state()
    if 'selected' in st:
        print ('got selected')
    if ck.instate('selected') :
        print('instate true')       
root = tk.Tk()
root.geometry('200x200+300+200')
root.grid_rowconfigure(0, weight = 1)

ck = ttk.Checkbutton(root,text = 'tryme')
ck.grid()
root.bind('<Return>',p)
root.mainloop()

The code should work now. ck.instate('selected) gives error and appears to only see the first character of the argument

Jim Robinson
  • 189
  • 1
  • 10
  • 1
    Please give us minimal reproducible code :) This example is not working - you did not defined `ck`. Provide the code and we can work from there :) – Jakub Szlaur Jan 22 '21 at 22:55
  • 2
    I believe you need to use `ck.instate(['selected'])` - a list of one or more state specifications, rather than a single state. – jasonharper Jan 22 '21 at 23:04
  • 1
    Check out this answer: https://stackoverflow.com/a/4236929/11101156 – Jakub Szlaur Jan 22 '21 at 23:08
  • What have you done to debug this? Have you verified that `ck.state()` is returning what you think it is returning, and is the type that you expect? – Bryan Oakley Jan 22 '21 at 23:35
  • jason harper comment is correct. That fixed the problem. Apparently instate expects a list, not a string. Thank You – Jim Robinson Jan 23 '21 at 04:40

0 Answers0