I am trying to pack two integer variables and write them to the serial port using struct.pack in Python. The variables are defined as integer variables but I keep getting the following error: 'struct.error: required argument is not an integer'
I have been successful at packing actual numbers, just not variables.
my code is
var1 = IntVar()
var2 = IntVar()
I assign integer values using a series of sliders I built in Tkinter and than call the command:
def Pace():
ser.write(struct.pack('!BB',var1,var2)
I'm sure I am missing something simple, but I can't seem to even force the variables to become integers with Int() or the equivalent.
Any help would be greatly appreciated.
Thank you
IntVar()
is not an integer - is a Tkinter object used to notifying observers when it's value changes.
To use it in struct pack you need to retrieve underlying primitive. struct.pack('!BB', var1.get(), var2.get())
.get()
method in docs ( emphasis mine ):
The get method returns the current value of the variable, as a Python object. For BooleanVar variables, the returned value is 0 for false, and 1 for true. For DoubleVar variables, the returned value is a Python float. For IntVar, it's an integer . For StringVar, it's either an ASCII string or a Unicode string, depending on the contents.
ser.write(struct.pack('!BB',var1.get(),var2.get())
我认为至少...也许只是var1(),var2()
...一段时间以来,因为我搞砸了tkinter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.