负浮动问题 - Python(Tkinter)

时间:2016-06-01 06:56:50

标签: python python-3.x tkinter

我在使用我的代码行时遇到问题,我可以执行正数但我无法在数字前面添加-符号以使其成为负数。

以下是代码:

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

这是您输入浮动的左侧输入框代码:

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)

感谢您的帮助!

修改

运行它的代码:

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

1 个答案:

答案 0 :(得分:0)

您可以检查字符串开头是否有减号字符,而不是直接将输入的文本转换为浮点数:

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))

由于self.Text_Length_Left.set('')在您这样做时清除了输入框,因此您无法输入任何其他数字。

希望它会有所帮助。