为什么 math.floor 不起作用?

Why will math.floor not work?

该程序的目标是使用 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" 仍被打印为浮点数。有什么建议吗?

int(math.floor(out))

这将创建 out 的整数版本,但您没有将它分配给任何东西,所以它只是被丢弃了。如果您希望更改反映在 out 的值中,请尝试:

out = int(math.floor(out))