代码之家  ›  专栏  ›  技术社区  ›  Pete Lane

tk代码错误,不支持的操作数

  •  0
  • Pete Lane  · 技术社区  · 6 年前

    冒犯者的评论 NewLevel 函数提供一些解释。

    fuelstats.txt 是一个包含一个数字的文件 94.5

    运行此代码时,错误为:

    unsupported operand - for float to StringVar
    

    这是给 new_level.set(fuel_level - kmltr) ,因为其中一个是浮点数,但我已经尝试了所有我能想到的方法来更改类型,我甚至尝试了 get() 也许是用错误的方式我不知道。

    ##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.
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Silvio Mayolo    6 年前

    您需要在 StringVar 然后转换 那个 到a float .

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