Regan Kelly Regan Kelly - 6 months ago 34
Python Question

Negative float Issue - Python (Tkinter)

I am having trouble with a line of my code where I can do positive numbers but I can't add a

-
sign in front of the numbers to make it a negative number.

Here is the code:

as_meter = dict(mm=0.001, cm=0.01, inch=0.0254,ft=0.3048, yd=0.9144, m=1.0,km=1000.0, mi=1609.344,)
def update_length(self, *args):
try:
v = float(self.Text_Length_Left.get())
except ValueError:
self.Text_Length_Left.set('')
self.Text_Length_Right.set('')
return


Here is the Left entry box code where you enter the float:

self.Text_Length_Left = StringVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Text_Length_Left.trace('w', self.update_length)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)


Thanks for any help!

Edit

Code to run it:

class Conversion:

as_meter = dict(mm=0.001, cm=0.01, inch=0.0254,ft=0.3048, yd=0.9144, m=1.0,km=1000.0, mi=1609.344,)
def update_length(self, *args):
try:
v = float(self.Text_Length_Left.get())
except ValueError:
self.Text_Length_Left.set('')
self.Text_Length_Right.set('')
return
m = v * self.as_meter [self.measurements.get()]
r = m/self.as_meter[self.measurements1.get()]
self.Text_Length_Right.set("{:.5g}".format(r))

class GUI(Conversion):
def __init__(self,root):
self.notebook = ttk.Notebook(root)
self.notebook.pack()

self.length_frame = ttk.Frame(self.notebook)
self.weight_frame = ttk.Frame(self.notebook)
self.temperature_frame = ttk.Frame(self.notebook)
self.help_frame = ttk.Frame(self.notebook)

self.notebook.add(self.length_frame, text = 'Length')

#Combobox
self.measurements = StringVar()
self.Combobox_Length_Left = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state=['readonly'])
self.Combobox_Length_Left.current(5)
self.measurements.trace('w', self.update_length)
self.Combobox_Length_Left.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = E)

self.measurements1 = StringVar()
self.Combobox_Length_Right = ttk.Combobox(self.length_frame, textvariable = self.measurements1, value = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state = ['readonly'])
self.Combobox_Length_Right.current(5)
self.measurements1.trace('w', self.update_length)
self.Combobox_Length_Right.grid(row = 2, column = 2, padx = 5, pady = 5, sticky = E)

#Labels
self.Conversion = ttk.Label(self.length_frame, text = 'Convertion:').grid(row = 1, column = 0, padx = 5, pady = 5, sticky = W)
self.Label_Blank = ttk.Label(self.length_frame, text = '').grid(row = 1, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To2 = ttk.Label(self.length_frame, text = 'to').grid(row = 2, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To = ttk.Label(self.length_frame, text = 'to').grid(row = 3, column = 1, padx = 5, pady = 5, sticky = E)

#Entry Boxes
self.Text_Length_Left = StringVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Text_Length_Left.trace('w', self.update_length)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)

self.Text_Length_Right = StringVar()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13, state='readonly')
self.Entry_Length_Right.grid(row = 3, column = 2, padx = 5, pady = 5)
enter code here

3kt 3kt
Answer

Instead of casting directly the entered text to float, you can check if there is a minus character at the beginning of the string:

def update_length(self, *args):

    entered_value = self.Text_Length_Left.get()

    if entered_value.startswith('-'):
        sign = -1
        entered_value = entered_value.replace('-', '', 1)
    else:
        sign = 1

    try:
        v = float(entered_value)
    except ValueError:
        self.Text_Length_Right.set('Invalid input')
        return
    m = v * sign * self.as_meter [self.measurements.get()]
    r = m/self.as_meter[self.measurements1.get()]
    self.Text_Length_Right.set("{:.5g}".format(r))

You were not able to input anything other that numbers because of the self.Text_Length_Left.set('') which cleared the input box when you did so.

Hope it'll be helpful.