0

該程序的目標是使用12位DAC產生正弦波。的代碼如下:爲什麼math.floor不起作用?

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() 

當我運行我收到以下錯誤值後程序「出」打印:

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」正在被打印爲float仍然。有什麼建議麼?

回答

3
int(math.floor(out)) 

這將創建out一個完整版本,但你不將其分配給任何東西,所以它只是被丟棄。如果要將更改反映在值out中,請嘗試:

out = int(math.floor(out))