TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.58k stars 1.08k forks source link

CTkTextbox .cget("state") not working #1212

Open STDM92 opened 1 year ago

STDM92 commented 1 year ago

I am trying to get the state of a textbox with the following code:

state = textbox.cget("state")

But I always get the following error: File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/customtkinter/windows/widgets/core_widget_classes/ctk_base_class.py", line 151, in cget raise ValueError(f"'{attribute_name}' is not a supported argument. Look at the documentation for supported arguments.") ValueError: 'state' is not a supported argument. Look at the documentation for supported arguments.

Setting the state with .configure works fine.

I've tried the same with CTkEntry and it works. According to the documentation "state" should be a supported argument, right? Or am I missing something here?

gem7008 commented 1 year ago

Same, I also have the same issue. I also tried .state() and got AttributeError: 'CTkTextbox' object has no attribute 'state'.

sethstenzel commented 4 months ago

This looks to be a bug where it is targeting the wrapping widget and not the inner.

I was able to work around this with: text_input.children['!text'].cget('state')

for example the following code:

print('1:', type(text_input))
print('2:', text_input.children['!text'].cget('state'))
text_input.configure(state='disabled')
print('3:', text_input.children['!text'].cget('state'))
text_input.configure(state='normal')
print('4:', text_input.children['!text'].cget('state'))

yields the following output:

1: <class 'customtkinter.windows.widgets.ctk_textbox.CTkTextbox'>
2: normal
3: disabled
4: normal
rtommy commented 2 months ago

I just met this problem last weekend too. Not only state but some other tkinter.Text attributes have the same issue. My workaround was slightly different but basically same concept.

textbox._textbox.cget('state')

Based to site-packages\customtkinter\windows\widgets\ctk_textbox.py the .cget() function only covers some attributes but not all of the tkinter.Text class attributes (obviously). So the code passes to parent via return super().cget(attribute_name) but that is CTkBaseClass which does not consider these cases either.

If I check the code of .configure() then the default behaviour to configure any attribute is self._textbox.configure(**pop_from_dict_by_set(kwargs, self._valid_tk_text_attributes)). That is where I got the _textbox idea from.

I have not tested yet but I would try a source code correction as permanent solution to change the default return behaviour of .cget() like this:

return self._textbox.cget(attribute_name)