0

I know that tkinter spinbox returns a set of values and we can use that value later by using .get method. But is it possible to create a tkinter spinbox which just returns 2 outputs (True or False) ?

def apply():
    if (toggle_var.get() == "ON"):
        # changing on/off state will start/stop the plot
        def dark():
            if (dark_var.get() == "ON"):
                print('do something from a function here')
            else:
                print('do some other thing')
            
        # Button to toggle between dark/no dark state
    
# Button to toggle between on/off state 

I am wondering if I can use a spinbox returning True or False can be used instead of the second checkbutton here.

  • Why not use a `tkinter.Radiobutton` or `tkinter.Checkbutton`? – TheLizzard Apr 16 '21 at 07:27
  • what stops You from doing that? – Matiiss Apr 16 '21 at 07:34
  • @TheLizzard well I guess spinbox takes up less space – Matiiss Apr 16 '21 at 07:34
  • @Matiiss `Spinbox`es are for `int`s, `Checkbutton`s are for `bool`s, `Text`s/`Entry`s are for strings. – TheLizzard Apr 16 '21 at 07:38
  • @TheLizzard ok but any of those data types can be "translated" to any other (it would be particularly hard with integers to strings but still possible) like it is possible to place just two values in spinbox and if it is one then return true and if the other return false or sth like that – Matiiss Apr 16 '21 at 07:40
  • @Matiiss `Spinbox`es weren't created for `bool`s. Tkinter has a lot of widgets (technecally they are OS's widgets) and all of them were designed with a datatype in mind. That is why you can't put `True` and `False` inside a `Spinbox`. Yes you can use `1` for `True` and `0` for `False` but that will be bad practise and the GUI isn't going to look good. – TheLizzard Apr 16 '21 at 07:45
  • But I thought it was possible to put string values in the spinbox, also I do agree it wouldn't be great to put something like that inside a spinbox (more because there wouldn't be much to spin), instead I would suggest Combobox which is somewhat similar if the looks is what the OP was going for – Matiiss Apr 16 '21 at 07:50
  • So basically what I need is two toggle buttons(so far I can only do one function with one checkbutton , when I try to add the other the GUI freezes). So I was wondering if I can use a spinbox and use `.get` method instead of the second checkbutton –  Apr 16 '21 at 07:54
  • @NicoleWaves Why does your GUI freeze? If you have 2 buttons (where 1 of them must be selected for the user input to be valid) you should use `tkinter.Radiobutton`. Is that what you need? If so look at [this](https://www.tutorialspoint.com/python/tk_radiobutton.htm) – TheLizzard Apr 16 '21 at 08:03
  • Yes you can: `var1 = tk.BooleanVar(); spin = tk.Spinbox(root, values=(False, True), wrap=True, textvariable=var1)`. Then you can use `var1.get()` to get the boolean value of the spinbox. But a `Checkbutton()` can do the same as the `Spinbox()` in this case. – acw1668 Apr 16 '21 at 08:19
  • 1
    I would recommend you try to implement a `Checkbutton` and then post the issue here. – Delrius Euphoria Apr 16 '21 at 08:55
  • Many thanks to all. I managed to add a second checkbutton without freezing the GUI as @acw1668 suggested. –  Apr 17 '21 at 00:59

0 Answers0