python-3.xtkinterttkbootstrapttkcombobox

float function is reversing a string number when converting to number in Python 3x?


I've cut this down to the basic functionality I'm looking to fix, as I have more than one combobox, but I'm having an issue when I select the last item in a tuple from a tkinter combobox and convert it to a float. Somehow it's reversing the number?

I have a similar prototype which works fine, but something in this code is causing this issue?

Here is the code:

import tkinter
from tkinter import SUNKEN
import ttkbootstrap as ttk
from ttkbootstrap.constants import *

def handle_selection(event):
    print("In the handle_selection method!")

    selected_index = ti_combobox.current()
    print("selected_index is: ")
    print(selected_index)
    selected_tuple =  ti_combobox.get()
    print("selected_tuple is: ")
    print(selected_tuple)
    selected_float = float(selected_tuple[-1]) # Here the float conversion reverses the number???
    print("selected_float is: ")
    print(selected_float)

window = ttk.Window(themename = 'superhero')
window.title('Basic Tool')
window.geometry('650x350')

main_frame = ttk.Frame(window, relief=SUNKEN, borderwidth=1)
main_frame.grid(row=0, column=0)

base_finding_frame = tkinter.LabelFrame(main_frame, text='BASE', relief=SUNKEN, borderwidth=1)
base_finding_frame.grid(row=4, column=0, sticky="news", padx=10, pady=15)

base_label_ti = ttk.Label(base_finding_frame, text='(TI)', font=('Helvetica', 10))
base_label_ti.grid(row=0, column=0)

string_var = ttk.StringVar()
ti_values = [("Crit", "C", "1.0"), ("High", "H", "0.8"), ("Medium", "M", "0.3"), ("Low", "L", "0.2"),  ("None", "N", "0.0"), ("Default", "D", "0.5"),  ("Unknown", "UK", "0.4"),  ("Not Applicable", "NA", "1.0")]
ti_combobox = ttk.Combobox(base_finding_frame, bootstyle='primary', values=ti_values, state = 'readonly', textvariable= string_var)
ti_combobox.grid(row=1, column=0)

ti_combobox.bind("<<ComboboxSelected>>", handle_selection)

# Run the GUI 
window.mainloop() 

Here is the output:
In the handle_selection method!
selected_index is:
0
selected_tuple is:
Crit C 1.0
selected_float is:
0.0 (This removed the digit one and replaced it with zero, which I wasn't expecting!)
In the handle_selection method!
selected_index is:
1
selected_tuple is:
High H 0.8
selected_float is:
8.0
In the handle_selection method!
selected_index is:
3
selected_tuple is:
Low L 0.2
selected_float is:
2.0

Any ideas to what I'm missing here?


Solution

  • The problem is that ti_combobox.get() is retrieving your values as a single string, not as the tuples you pass when you create the widget.

    So, the call to float is actually picking the last character you see on the displayed value, and that is resulting in the behavior you printed out.

    Just split the string retrieved on the white spaces, before calling float, in order to get the whole number. Replace the line selected_float = float(selected_tuple[-1]) with

    selected_float = float(selected_tuple.split()[-1])

    and it should work.