Search code examples
pythontkinterttk

tk code error, unsupported operand


The comments in the offending NewLevel function provide some explanation.

fuelstats.txt is a file with one number in it 94.5

When I run this code the error is:

unsupported operand - for float to StringVar

this is for new_level.set(fuel_level - kmltr), because one is a float but I've tried everything I can think of to change the types, I've even tried a get() maybe in the wrong way I don't know.

##call the tkinter module first with all libraries
##call the ttk module next but without all libraries so that
##I can use different lib as I need to
##...without the ttk lib I need to explicitly call the ttk function
from tkinter import *
from tkinter import ttk

##import fuel level from file
file = 'fuelstats.txt'
stats = open("fuelstats.txt").readlines()
##assign fuellevel to variable
fuel_level = stats[-1]
fuel_level = float(fuel_level)
ratekm = 2.5


###defining the calculate function here because it needs to be
###referenced early in the code
def calculate(*args):
    value = float(km.get())
    kmltr.set(value / ratekm)
    ##call the next function
    NewLevel()

def NewLevel(*args):
    global new_level
    ##this function does not work, it throws an exception
    ## that I cannot subtract a float from a stringVar
    ## this is obvious but how do I define the types properly?
    new_level.set(fuel_level - kmltr)


def km_left():
    global reserves
    reserves = fuel_level * 2.5
    n = 2
    reserves = '{:.{}f}'.format(reserves, n)

root = Tk() ##set up main window container
root.title("Calculate Fuel")## give it a title

##next set up a frame widget which holds all the content
mainframe = ttk.Frame(root, padding="30 13 20 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
##these two lines tell tk to resize with main window
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)



##define variables
km = StringVar()
kmltr = StringVar()
reserves = StringVar()
new_level = StringVar()

##call to functions
km_left()

##next create the three main widgets - input field for km
km_entry = ttk.Entry(mainframe, width=7, textvariable=km)
km_entry.grid(column=2, row=3, sticky=(W,E))

##the result fields (as labels) and the calculate button
ttk.Label(mainframe, text=fuel_level).grid(column=2, row=1, sticky=W)
ttk.Label(mainframe, text=reserves).grid(column=2, row=2, sticky=W)
ttk.Label(mainframe, textvariable=kmltr).grid(column=2, row=4, sticky=W)
ttk.Label(mainframe, text=new_level).grid(column=2, row=5, sticky=W)
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=5, row=6, sticky=W)

##below I create labels for the widgets and place them in a the grid
ttk.Label(mainframe, text="km").grid(column=3, row=2, sticky=W)
ttk.Label(mainframe, text="litres").grid(column=3, row=1, sticky=E)
ttk.Label(mainframe, text="litres").grid(column=3, row=4, sticky=E)
ttk.Label(mainframe, text="fuel level is: ").grid(column=1, row=1, sticky=W)
ttk.Label(mainframe, text="fuel reserves = ").grid(column=1, row=2, sticky=W)
ttk.Label(mainframe, text="enter km traveled ").grid(column=1, row=3, sticky=W)
ttk.Label(mainframe, text="fuel used is: ").grid(column=1, row=4, sticky=W)
ttk.Label(mainframe, text="new fuel level is: ").grid(column=1, row=5, sticky=W)

##below is a loop to put padding around each field and widget
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

km_entry.focus() ##focus of the first field
root.bind('<Return>', calculate)##and allow the enter key to act as the button

root.mainloop()
##This final line tells Tk to enter its event loop,
## which is needed to make everything run.

Solution

  • You need to get the value inside the StringVar and then convert that to a float.

    new_level.set(fuel_level - float(kmltr.get()))