为什么math.floor不起作用?

时间:2016-03-01 20:12:40

标签: python floating-point integer type-conversion raspberry-pi2

该程序的目标是使用12位DAC生成正弦波。代码如下:

from Tkinter import *
import smbus, time, math, random

bus = smbus.SMBus(1)
address = 0x60
t=time.time()

class RPiRFSigGen:
        # Build Graphical User Interface
        def __init__(self, master):

                self.start

                frame = Frame(master, bd=10)
                frame.pack(fill=BOTH,expand=1)
                # set output frequency
                frequencylabel = Label(frame, text='Frequency (Hz)', pady=10)
                frequencylabel.grid(row=0, column=0)
                self.frequency = StringVar()
                frequencyentry = Entry(frame, textvariable=self.frequency, width=10)
                frequencyentry.grid(row=0, column=1)
                # Start button
                startbutton = Button(frame, text='Enter', command=self.start)
                startbutton.grid(row=1, column=0)


        def start(self):
                #self.low_freq=IntVar
                low_freq = float(self.frequency.get())
                out = 4095/2 + (math.sin(2*math.pi*low_freq*t))
                #out = math.floor(out)
                int(math.floor(out))
                print (out)
                bus.write_byte_data(address,0,out)
                sendFrequency(low_freq)

# Assign TK to root
root = Tk()

# Set main window title
root.wm_title('DAC Controller')
root.geometry('250x150+650+250')
# Create instance of class RPiRFSigGen
app = RPiRFSigGen(root)

# Start main loop and wait for input from GUI
root.mainloop()

当我运行程序时,我在值" out"之后收到以下错误打印出来:

2046.18787764
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1437, in __call__
    return self.func(*args)
  File "/home/pi/DAC Controller.py", line 40, in start
    bus.write_byte_data(address,0,out)
TypeError: integer argument expected, got float

int(math.floor(out))似乎没有转换为整数,因为" out"正在浮动打印。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

int(math.floor(out))

这将创建out的整数版本,但您不会将其分配给任何内容,因此它会被丢弃。如果您希望更改反映在out的值中,请尝试:

out = int(math.floor(out))