_tkinter.TclError: expected floating-point number but got “08” (looks like invalid octal number)

前端 未结 1 2018
慢半拍i
慢半拍i 2021-01-27 04:00

I am an A-level student writing his coursework for computing. A small part of my codes takes in inputs are turns them into a time. 8 o\'clock in the morning is shown like this:

相关标签:
1条回答
  • 2021-01-27 04:49

    It's your IntVar. I don't know if it's a bug or for backwards compatibility, but IntVar.get() does type conversion for you. In Python 2, numbers prefixed by 0 would be interpreted in octal (i.e. 010 equals 8). You're setting the value just fine, but it blows up when it tries to get() "08".

    Searching through the code, the problem stems from the actual Tcl interpreter, since the get() method is sent to the Tcl interpreter as globalgetvar(), and from there it's not even interpreted by Python. In reality, it isn't even a bug- Tcl is supposed to recognize 0### as octal, and this is considered one of the gotcha's of the language.

    The proper workaround for this problem is to use a good data access object- use it to send and retrieve data to and from sqlite. This way you can encapsulate code to transform an integer 8 to the 08 that sqlite wants. The slightly more hacky way is to call a single function before you send data to sqlite, which looks something like:

    anInt = variable_start.get()
    serialInt = serialize(anInt)
    send_to_sqlite(serialInt) // I've not worked with SQLite, so this line is pseudocode.
    
    serialize(int):
        new = str(int)
        if(new < 10):
            new = "0" + new
        return new
    
    0 讨论(0)
提交回复
热议问题